package com.tangcredit.tdfs3.redis; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.tangcredit.tdfs3.utils.RedisUtils; import redis.clients.jedis.Jedis; @Component public class RedisNotifyDaemon implements InitializingBean,DisposableBean{ @Override public void destroy(){} @Autowired private RedisSub sub; @Override public void afterPropertiesSet() throws Exception { Jedis jedis = RedisUtils.getInstance(); new Thread(() -> { jedis.psubscribe(sub, "__keyevent@*__:expired"); }).start(); } }
package com.tangcredit.tdfs3.redis; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.tangcredit.tdfs3.dao.LoanInvestmentsDao; import com.tangcredit.tdfs3.dao.LoanPrjDynamicsDao; import com.tangcredit.tdfs3.dao.LoanProjectsDao; import com.tangcredit.tdfs3.entity.LoanInvestments; import com.tangcredit.tdfs3.entity.LoanPrjDynamics; import com.tangcredit.tdfs3.entity.LoanProjects; import com.tangcredit.tdfs3.utils.BigDecimalUtils; import com.tangcredit.tdfs3.utils.ConfigUtils; import redis.clients.jedis.JedisPubSub; @Component public class RedisSub extends JedisPubSub { @Autowired private LoanInvestmentsDao loanInvestmentsDao; @Autowired private LoanPrjDynamicsDao loanPrjDynamicsDao; @Autowired private LoanProjectsDao loanProjectsDao; /** * 精确匹配 * * @param channel * @param message */ @Override public void onMessage(String channel, String message) { System.out.println("redis sub :" + channel + " message " + message); } /** * 初始化按表达式的方式订阅时候的处理 * * @param pattern * @param subscribedChannels */ @Override public void onPSubscribe(String pattern, int subscribedChannels) { System.out.println(pattern + "=" + subscribedChannels); } /** * 按表达式的方式订阅的消息后的处理 * * @param pattern 模式 * @param channel 频道,通道 * @param message 信息 */ @Override public void onPMessage(String pattern, String channel, String message) { System.out.println(pattern + "=" + channel + "=" + message); // 失效的投资订单回滚 System.out.println("==================start"); if (message.indexOf(ConfigUtils.identifyingIds) == 0) { System.out.println("==================start1" + message + "========="); List<LoanInvestments> invests = this.loanInvestmentsDao.getInvestsByOrderId(message); if (invests != null && invests.size() > 0) { LoanInvestments invest = invests.get(0); System.out.println("==================start2" + invest.getId() + "=========" + invest.getInvestState() + "===============" + invest.getInvestAmount()); if (invest.getInvestState() != null && invest.getInvestState() == -100) { invest.setInvestState(null); this.loanInvestmentsDao.save(invest); // save invest Integer loanId = invest.getLoanId(); LoanPrjDynamics dynamics = this.loanPrjDynamicsDao.getByLoanId(loanId); LoanProjects lp = this.loanProjectsDao.findOne(loanId); // success BigDecimal successAmount = this.loanInvestmentsDao.getInvestAmountByLoanIdAndState(loanId, 100); BigDecimal freezeAmount = this.loanInvestmentsDao.getInvestAmountByLoanIdAndState(loanId, -100); BigDecimal hasAmount = BigDecimalUtils.add(successAmount, freezeAmount); dynamics.setHasAmount(hasAmount); BigDecimal loanProgress = hasAmount .divide(lp.getLoanAmount(), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)); dynamics.setLoanProgress(loanProgress.intValue()); if (lp.getProjectState() == 4) { lp.setProjectState(2); this.loanProjectsDao.save(lp); } this.loanPrjDynamicsDao.save(dynamics); System.out.println("==================SUCCESS=========================="); } } } } }
RedisUtils.setex(orderId, 60, String.valueOf(loanID)); // redis
http://blog.youkuaiyun.com/topwqp/article/details/8681573
http://redisdoc.com/topic/notification.html
http://blog.youkuaiyun.com/lihao21/article/details/48370687