服务器端:
/**
* 进入通道
* @param channel 通道名称
* @param message 序列化数据(byte数组)
*/
@Override
public void sendMessage(String channel, Serializable message) {
redisTemplate.convertAndSend(channel, message);
}
客户端:
配置:
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:redis="http://www.springframework.org/schema/redis" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd" default-lazy-init="false"> <description>jedis Configuration</description> <!-- Redis配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <!-- Redis工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.ip}" /> <property name="port" value="${redis.port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <!-- Spring自带Redis工厂 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" /> <!-- 实现类 --> <bean id="messageDelegateListener" class="com.fgoods.siteanalyzer.main.listener.impl.MessageDelegateListenerImpl" ></bean> <!-- 序列化工具 --> <bean id="serialization" class="com.fgoods.siteanalyzer.main.listener.JSONRedisSerializer" /> <!-- 监听器 --> <bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter"> <property name="delegate" ref="messageDelegateListener" /> <property name="serializer" ref="serialization" /> </bean> <!-- Redis通道 --> <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="messageListeners"> <map> <entry key-ref="messageListener"> <bean class="org.springframework.data.redis.listener.ChannelTopic"> <constructor-arg value="bifilter" /> <!-- 这里配置消费端需要订阅的频道,可以是多个。该一例子订阅JAVA这个频道 --> </bean> </entry> </map> </property> </bean> </beans>
代码:
package com.fgoods.siteanalyzer.main.listener.impl;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.scheduling.annotation.Async;
/**
* @author LiXiangDong
*
*/
public class MessageDelegateListenerImpl implements MessageListener {
private Converter<byte[], Object> deserializer = new DeserializingConverter();
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] obj = (byte[]) deserializer.convert(message.getBody());
//解析数据入库
analytical(new String(obj));
}
/**
* 解析数据,并将数据入库
* @param source
*/
@Async
private void analytical(String source) {
//解析数据
}
}
序列化工具:
package com.fgoods.siteanalyzer.main.listener;
import org.hibernate.type.SerializationException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* @author LiXiangDong
*
*/
public class JSONRedisSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
public Object deserialize(byte[] bytes) {
if (null == bytes || bytes.length == 0) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
public byte[] serialize(Object object) {
if (object == null) {
return new byte[0];
}
try {
return serializer.convert(object);
} catch (Exception ex) {
throw new SerializationException("Cannot serialize", ex);
}
}
}