diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisCacheStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisCacheStorager.java index b51d9e39..de084bd4 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisCacheStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisCacheStorager.java @@ -2,14 +2,14 @@ package com.foxinmy.weixin4j.cache; import java.util.Set; +import com.foxinmy.weixin4j.util.Consts; +import com.foxinmy.weixin4j.util.SerializationUtils; + import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.util.Pool; -import com.foxinmy.weixin4j.util.Consts; -import com.foxinmy.weixin4j.util.SerializationUtils; - /** * 用Redis保存缓存对象(推荐使用) * @@ -18,124 +18,117 @@ import com.foxinmy.weixin4j.util.SerializationUtils; * @date 2015年1月9日 * @since JDK 1.6 */ -public class RedisCacheStorager implements - CacheStorager { +public class RedisCacheStorager implements CacheStorager { - private Pool jedisPool; + private Pool jedisPool; - private final static String HOST = "127.0.0.1"; - private final static int PORT = 6379; - private final static int TIMEOUT = 5000; - private final static int MAX_TOTAL = 50; - private final static int MAX_IDLE = 5; - private final static int MAX_WAIT_MILLIS = 5000; - private final static boolean TEST_ON_BORROW = false; - private final static boolean TEST_ON_RETURN = true; + private final static String HOST = "127.0.0.1"; + private final static int PORT = 6379; + private final static int TIMEOUT = 5000; + private final static int MAX_TOTAL = 50; + private final static int MAX_IDLE = 5; + private final static int MAX_WAIT_MILLIS = 5000; + private final static boolean TEST_ON_BORROW = false; + private final static boolean TEST_ON_RETURN = true; + private final static JedisPoolConfig POOLCONFIG; + static { + POOLCONFIG = new JedisPoolConfig(); + POOLCONFIG.setMaxTotal(MAX_TOTAL); + POOLCONFIG.setMaxIdle(MAX_IDLE); + POOLCONFIG.setMaxWaitMillis(MAX_WAIT_MILLIS); + POOLCONFIG.setTestOnBorrow(TEST_ON_BORROW); + POOLCONFIG.setTestOnReturn(TEST_ON_RETURN); + } - public RedisCacheStorager() { - this(HOST, PORT, TIMEOUT); - } + public RedisCacheStorager() { + this(HOST, PORT, TIMEOUT); + } - public RedisCacheStorager(String host, int port, int timeout) { - JedisPoolConfig jedisPoolConfig = defaultConfig(); - this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); - } - - public RedisCacheStorager(String host, int port, int timeout, String password) { - JedisPoolConfig jedisPoolConfig = defaultConfig(); - this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); - } - - private JedisPoolConfig defaultConfig(){ - JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); - jedisPoolConfig.setMaxTotal(MAX_TOTAL); - jedisPoolConfig.setMaxIdle(MAX_IDLE); - jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); - jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); - jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN); - return jedisPoolConfig; - } + public RedisCacheStorager(String host, int port, int timeout) { + this(host, port, timeout, null, POOLCONFIG); + } - public RedisCacheStorager(JedisPoolConfig jedisPoolConfig) { - this(new JedisPool(jedisPoolConfig, HOST, PORT, TIMEOUT)); - } + public RedisCacheStorager(String host, int port, int timeout, String password) { + this(host, port, timeout, password, POOLCONFIG); + } - public RedisCacheStorager(String host, int port, int timeout, - JedisPoolConfig jedisPoolConfig) { - this(new JedisPool(jedisPoolConfig, host, port, timeout)); - } + public RedisCacheStorager(JedisPoolConfig poolConfig) { + this(new JedisPool(poolConfig, HOST, PORT, TIMEOUT)); + } - public RedisCacheStorager(Pool jedisPool) { - this.jedisPool = jedisPool; - } + public RedisCacheStorager(String host, int port, int timeout, String password, JedisPoolConfig poolConfig) { + this(new JedisPool(poolConfig, host, port, timeout, password)); + } - @SuppressWarnings("unchecked") - @Override - public T lookup(String key) { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - byte[] value = jedis.get(key.getBytes(Consts.UTF_8)); - return value != null ? (T) SerializationUtils.deserialize(value) - : null; - } finally { - if (jedis != null) { - jedis.close(); - } - } - } + public RedisCacheStorager(Pool jedisPool) { + this.jedisPool = jedisPool; + } - @Override - public void caching(String key, T cache) { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - byte[] cacheKey = key.getBytes(Consts.UTF_8); - byte[] value = SerializationUtils.serialize(cache); - if (cache.getExpires() > 0) { - jedis.setex(cacheKey, - (int) (cache.getExpires() - CUTMS) / 1000, value); - } else { - jedis.set(cacheKey, value); - } - jedis.sadd(ALLKEY, key); - } finally { - if (jedis != null) { - jedis.close(); - } - } - } + @SuppressWarnings("unchecked") + @Override + public T lookup(String key) { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + byte[] value = jedis.get(key.getBytes(Consts.UTF_8)); + return value != null ? (T) SerializationUtils.deserialize(value) : null; + } finally { + if (jedis != null) { + jedis.close(); + } + } + } - @Override - public T evict(String key) { - T cache = lookup(key); - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - jedis.del(key); - jedis.srem(ALLKEY, key); - } finally { - if (jedis != null) { - jedis.close(); - } - } - return cache; - } + @Override + public void caching(String key, T cache) { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + byte[] cacheKey = key.getBytes(Consts.UTF_8); + byte[] value = SerializationUtils.serialize(cache); + if (cache.getExpires() > 0) { + jedis.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000, value); + } else { + jedis.set(cacheKey, value); + } + jedis.sadd(ALLKEY, key); + } finally { + if (jedis != null) { + jedis.close(); + } + } + } - @Override - public void clear() { - Jedis jedis = null; - try { - jedis = jedisPool.getResource(); - Set cacheKeys = jedis.smembers(ALLKEY); - if (!cacheKeys.isEmpty()) { - cacheKeys.add(ALLKEY); - jedis.del(cacheKeys.toArray(new String[cacheKeys.size()])); - } - } finally { - if (jedis != null) { - jedis.close(); - } - } - } + @Override + public T evict(String key) { + T cache = lookup(key); + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + jedis.del(key); + jedis.srem(ALLKEY, key); + } finally { + if (jedis != null) { + jedis.close(); + } + } + return cache; + } + + @Override + public void clear() { + Jedis jedis = null; + try { + jedis = jedisPool.getResource(); + Set cacheKeys = jedis.smembers(ALLKEY); + if (!cacheKeys.isEmpty()) { + cacheKeys.add(ALLKEY); + jedis.del(cacheKeys.toArray(new String[cacheKeys.size()])); + } + } finally { + if (jedis != null) { + jedis.close(); + } + } + } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisClusterCacheStorager.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisClusterCacheStorager.java index 9b24cc96..7c97275c 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisClusterCacheStorager.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/cache/RedisClusterCacheStorager.java @@ -30,14 +30,14 @@ public class RedisClusterCacheStorager implements private final JedisCluster jedisCluster; public RedisClusterCacheStorager(Set nodes) { - JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); - jedisPoolConfig.setMaxTotal(MAX_TOTAL); - jedisPoolConfig.setMaxIdle(MAX_IDLE); - jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); - jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); - jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN); + JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxTotal(MAX_TOTAL); + poolConfig.setMaxIdle(MAX_IDLE); + poolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); + poolConfig.setTestOnBorrow(TEST_ON_BORROW); + poolConfig.setTestOnReturn(TEST_ON_RETURN); this.jedisCluster = new JedisCluster(nodes, CONNECTION_TIMEOUT, - SO_TIMEOUT, MAX_REDIRECTIONS, jedisPoolConfig); + SO_TIMEOUT, MAX_REDIRECTIONS, poolConfig); } public RedisClusterCacheStorager(Set nodes, diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java index eaf311b7..2391a937 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java @@ -1,5 +1,8 @@ package com.foxinmy.weixin4j.payment.mch; +import java.util.HashMap; +import java.util.Map; + import com.foxinmy.weixin4j.model.WeixinPayAccount; import com.foxinmy.weixin4j.payment.PayRequest; import com.foxinmy.weixin4j.type.SignType; @@ -9,64 +12,62 @@ import com.foxinmy.weixin4j.util.MapUtil; /** * APP支付 - * + * * @className APPPayRequest * @author jinyu(foxinmy@gmail.com) * @date 2015年12月25日 * @since JDK 1.6 * @see com.foxinmy.weixin4j.payment.mch.PrePay * @see com.foxinmy.weixin4j.payment.PayRequest - * @see APP支付 + * @see APP支付 */ public class APPPayRequest extends AbstractPayRequest { - public APPPayRequest(String prePayId, WeixinPayAccount payAccount) { - super(prePayId, payAccount); - } + public APPPayRequest(String prePayId, WeixinPayAccount payAccount) { + super(prePayId, payAccount); + } - @Override - public TradeType getPaymentType() { - return TradeType.APP; - } + @Override + public TradeType getPaymentType() { + return TradeType.APP; + } - /** - * 只做查看之用,请不要尝试作为支付请求 - */ - @Override - public PayRequest toRequestObject() { - PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), - "Sign=WXPay"); - payRequest.setPartnerId(getPaymentAccount().getMchId()); - payRequest.setPrepayId(getPrePayId()); - String sign = DigestUtil.MD5( - String.format("%s&key=%s", - MapUtil.toJoinString(payRequest, false, true), - getPaymentAccount().getPaySignKey())).toUpperCase(); - payRequest.setPaySign(sign); - payRequest.setSignType(SignType.MD5); - return payRequest; - } + /** + * 只做查看之用,请不要尝试作为支付请求 + */ + @Override + public PayRequest toRequestObject() { + PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), "Sign=WXPay"); + payRequest.setPartnerId(getPaymentAccount().getMchId()); + payRequest.setPrepayId(getPrePayId()); + Map map = new HashMap(); + map.put("appid", getPaymentAccount().getId()); + map.put("partnerid", getPaymentAccount().getMchId()); + map.put("prepayid", getPrePayId()); + map.put("package", payRequest.getPackageInfo()); + map.put("timestamp", payRequest.getTimeStamp()); + map.put("noncestr", payRequest.getNonceStr()); + String sign = DigestUtil.MD5( + String.format("%s&key=%s", MapUtil.toJoinString(map, false, true), getPaymentAccount().getPaySignKey())) + .toUpperCase(); + payRequest.setPaySign(sign); + payRequest.setSignType(SignType.MD5); + return payRequest; + } - @Override - public String toRequestString() { - PayRequest payRequest = toRequestObject(); - StringBuilder content = new StringBuilder(); - content.append(""); - content.append(String.format("", - payRequest.getAppId())); - content.append(String.format("", - getPaymentAccount().getPartnerId())); - content.append(String.format("", - payRequest.getPrepayId())); - content.append(String.format("", - payRequest.getPackageInfo())); - content.append(String.format("", - payRequest.getNonceStr())); - content.append(String.format("", - payRequest.getTimeStamp())); - content.append(String.format("", - payRequest.getPaySign())); - content.append(""); - return content.toString(); - } + @Override + public String toRequestString() { + PayRequest payRequest = toRequestObject(); + StringBuilder content = new StringBuilder(); + content.append(""); + content.append(String.format("", payRequest.getAppId())); + content.append(String.format("", getPaymentAccount().getPartnerId())); + content.append(String.format("", payRequest.getPrepayId())); + content.append(String.format("", payRequest.getPackageInfo())); + content.append(String.format("", payRequest.getNonceStr())); + content.append(String.format("", payRequest.getTimeStamp())); + content.append(String.format("", payRequest.getPaySign())); + content.append(""); + return content.toString(); + } }