消息发布与订阅概述
消息订阅发布模型如图所示
消息生产者负责消息的发布,通过约定的通信方式,让消费者消费相应的消息。下面使用redis简单了实现了消息的订阅与发布。
1.消息的订阅方
1.1配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:redis="http://www.springframework.org/schema/redis"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="localhost" p:port="6379" p:usePool="true">
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="redisConnectionFactory"/>
<bean id="redisDAO" class="redis.springredis.daoimpl.RedisDAOImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
<bean id="listener" class="redis.springredis.listern.MessageDelegateListenerImpl"/>
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<redis:listener-container>
<redis:listener ref="listener" serializer="jdkSerializer" method="handleMessage" topic="java" />
</redis:listener-container>
</beans>
1.2 消息监听接口和实现类
1.2.1监听接口
public interface MessageDelegateListener {
void handleMessage(Serializable message);
}
1.2.2实现类
public class MessageDelegateListenerImpl implements MessageDelegateListener {
public void handleMessage(Serializable message) {
if(message == null){
System.out.println("null");
} else if(message.getClass().isArray()){
System.out.println(Arrays.toString((Object[])message));
} else if(message instanceof List<?>) {
System.out.println(message);
} else if(message instanceof Map<? , ?>) {
System.out.println(message);
} else {
System.out.println(message.toString());
}
}
}
1.3 测试类
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:spring.redis.xml");
while (true) {
try {
System.out.println("current time: " + new Date());
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.消息发布
2.1配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="localhost" p:port="6379" p:usePool="true">
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="redisConnectionFactory"/>
<bean id="redisDAO" class="redistest.RedisDAOImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>
2.2 消息的发布测试
2.2.1 测试接口
public interface RedisDAO {
void sendMessage(String channel, Serializable message);
}
2.2.2测试接口实现类
@Service
public class RedisDAOImpl implements RedisDAO {
private RedisTemplate<String, Object> redisTemplate = null;
public RedisDAOImpl() {
}
public void sendMessage(String channel, Serializable message) {
redisTemplate.convertAndSend(channel, message);
}
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
2.2.3 测试发布消息程序
public class RedisProducer {
@Autowired
RedisDAO redisDAO;
@Test
public void testPublishMessage() throws Exception {
String msg = "Hello, Redis!";
redisDAO.sendMessage("java", msg);
Integer[] values = new Integer[]{21341,123123,12323};
redisDAO.sendMessage("java", values);
}
至此redis与spring整合的消息订阅与发布程序已经结束,后续补充理论知识。