相应redis中key值过期相应机制

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.csdn.net/topwqp/article/details/8681573
http://redisdoc.com/topic/notification.html
http://blog.csdn.net/lihao21/article/details/48370687  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值