|
| 1 | +package com.binarywang.solon.wxjava.channel.config.storage; |
| 2 | + |
| 3 | + |
| 4 | +import com.binarywang.solon.wxjava.channel.properties.RedisProperties; |
| 5 | +import com.binarywang.solon.wxjava.channel.properties.WxChannelProperties; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import me.chanjar.weixin.channel.config.WxChannelConfig; |
| 8 | +import me.chanjar.weixin.channel.config.impl.WxChannelRedisConfigImpl; |
| 9 | +import me.chanjar.weixin.common.redis.JedisWxRedisOps; |
| 10 | +import me.chanjar.weixin.common.redis.WxRedisOps; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.noear.solon.annotation.Bean; |
| 13 | +import org.noear.solon.annotation.Condition; |
| 14 | +import org.noear.solon.annotation.Configuration; |
| 15 | +import org.noear.solon.core.AppContext; |
| 16 | +import redis.clients.jedis.JedisPool; |
| 17 | +import redis.clients.jedis.JedisPoolConfig; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author <a href="https://github.com/lixize">Zeyes</a> |
| 21 | + * @author noear |
| 22 | + */ |
| 23 | +@Configuration |
| 24 | +@Condition( |
| 25 | + onProperty = "${"+WxChannelProperties.PREFIX + ".configStorage.type} = jedis", |
| 26 | + onClass = JedisPool.class |
| 27 | +) |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class WxChannelInJedisConfigStorageConfiguration extends AbstractWxChannelConfigStorageConfiguration { |
| 30 | + private final WxChannelProperties properties; |
| 31 | + private final AppContext applicationContext; |
| 32 | + |
| 33 | + @Bean |
| 34 | + @Condition(onMissingBean=WxChannelConfig.class) |
| 35 | + public WxChannelConfig wxChannelConfig() { |
| 36 | + WxChannelRedisConfigImpl config = getWxChannelRedisConfig(); |
| 37 | + return this.config(config, properties); |
| 38 | + } |
| 39 | + |
| 40 | + private WxChannelRedisConfigImpl getWxChannelRedisConfig() { |
| 41 | + RedisProperties redisProperties = properties.getConfigStorage().getRedis(); |
| 42 | + JedisPool jedisPool; |
| 43 | + if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { |
| 44 | + jedisPool = getJedisPool(); |
| 45 | + } else { |
| 46 | + jedisPool = applicationContext.getBean(JedisPool.class); |
| 47 | + } |
| 48 | + WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
| 49 | + return new WxChannelRedisConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix()); |
| 50 | + } |
| 51 | + |
| 52 | + private JedisPool getJedisPool() { |
| 53 | + WxChannelProperties.ConfigStorage storage = properties.getConfigStorage(); |
| 54 | + RedisProperties redis = storage.getRedis(); |
| 55 | + |
| 56 | + JedisPoolConfig config = new JedisPoolConfig(); |
| 57 | + if (redis.getMaxActive() != null) { |
| 58 | + config.setMaxTotal(redis.getMaxActive()); |
| 59 | + } |
| 60 | + if (redis.getMaxIdle() != null) { |
| 61 | + config.setMaxIdle(redis.getMaxIdle()); |
| 62 | + } |
| 63 | + if (redis.getMaxWaitMillis() != null) { |
| 64 | + config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
| 65 | + } |
| 66 | + if (redis.getMinIdle() != null) { |
| 67 | + config.setMinIdle(redis.getMinIdle()); |
| 68 | + } |
| 69 | + config.setTestOnBorrow(true); |
| 70 | + config.setTestWhileIdle(true); |
| 71 | + |
| 72 | + return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), redis.getDatabase()); |
| 73 | + } |
| 74 | +} |
0 commit comments