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);
}
}
}