spring-data-redis队列

本文介绍了如何使用Spring Data Redis实现消息队列,强调了依赖的添加以及消费者端的配置,特别是消费端需要设置轮询机制来确保能接收到发布的消息。
摘要由CSDN通过智能技术生成

1、首先加入依赖

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.4.RELEASE</version>
        </dependency>
2、编写MessageListener

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * Created with IntelliJ IDEA
 * ProjectName: omp
 * CreateUser:  sampson
 * CreateTime : 2016/12/7
 * ModifyUser: sampson
 * Class Description:
 * To change this template use File | Settings | File Template
 */
public class RedisMessageListener implements MessageListener{
    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println( "Received by RedisMessageListener: " + message.toString() );
    }
}
3、编写消息委托接口

import java.io.Serializable;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * ProjectName: omp
 * CreateUser:  sampson
 * CreateTime : 2016/12/7
 * ModifyUser: sampson
 * Class Description:
 * To change this template use File | Settings | File Template
 */
public interface MessageDelegate {

    public void handleMessage(String message);

    public void handleMessage(Map<?, ?> message);

    public void handleMessage(byte[] message);

    public void handleMessage(Serializable message);

    public void handleMessage(Serializable message, String channel);
}

4、编写实现类

import java.io.Serializable;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * ProjectName: omp
 * CreateUser:  sampson
 * CreateTime : 2016/12/7
 * ModifyUser: sampson
 * Class Description:
 * To change this template use File | Settings | File Template
 */
public class DefaultMessageDelegate implements MessageDelegate {

    @Override
    public void handleMessage(String message) {
        System.out.println("handleMessage(String message):" + message);
    }

    @Override
    public void handleMessage(Map<?, ?> message) {
        System.out.println("handleMessage(Map<?, ?> message):" + message);
    }

    @Override
    public void handleMessage(byte[] message) {
        System.out.println("handleMessage(byte[] message):" + new String(message));
    }

    @Override
    public void handleMessage(Serializable message) {
        System.out.println("handleMessage(Serializable message):"+ message.toString());
    }

    @Override
    public void handleMessage(Serializable message, String channel) {
        System.out.println("handleMessage:"+ " \t at " + new Date());
    }

5、添加spring配置

<bean id="msgListener" class="com.netease.ad.omp.listener.RedisMessageListener" />
	<bean id="msgDelegateListener" class="com.netease.ad.omp.listener.DefaultMessageDelegate" />

	<redis:listener-container connection-factory="connectionFactory">
		<redis:listener ref="msgListener" topic="${queue_name}" />
		<redis:listener ref="msgDelegateListener" method="handleMessage" topic="nex_adv_audit_queue" />
	</redis:listener-container>

	<bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
		<constructor-arg>
			<bean class="com.netease.ad.omp.listener.RedisMessageListener" />
		</constructor-arg>
	</bean>

	<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="messageListeners">
			<map>
				<entry key-ref="messageListener">
					<list>
						<bean class="org.springframework.data.redis.listener.ChannelTopic">
							<constructor-arg value="${queue_name}" />
						</bean>
					</list>
				</entry>
			</map>
		</property>
	</bean>

此方式只支持publish/subscribe的方式,要不然接收不到发送的消息。

下面是push/pop的方式

生产者的代码比较简单,此处就不写了,只写消费端,需要注意的一点是需要添加轮询机制才可以消费到消息。

public void consume(){
        Jedis jedis = null;
        try {
             //获取redis连接
            jedis = RedisUtils.getJRedis();
            while (true){
                List<String> messageJson = jedis.blpop(TIMEOUT, redisQueueConfig.get("nex.adv.audit.queue"));
                if (messageJson.get(1) != null) {
                    JSONObject jsonObj = JSONObject.parseObject(messageJson.get(1));
                    String json = JSON.toJSONString(jsonObj);
                    logger.info("$$$$$$$$$$$    Receive message is {} , at {} ",messageJson.get(1), DateUtils.getCurrentDate()+"     $$$$$$$$$$$");
                    //execute busniss code
                }
            }
        } finally {
            if (jedis != null) {
                RedisUtils.returnResource(jedis);
            }
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值