redis消息队列

spirng配置

<bean id="channelTopic" class="org.springframework.data.redis.listener.ChannelTopic">
	<constructor-arg value="channelTopic" />
</bean>

<bean id="myMessageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
	<constructor-arg>
		<bean class="com.dongnao.MyMessageListener"/>
	</constructor-arg>
</bean>

<bean id="topicContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
	<property name="connectionFactory" ref="jedisConnFactory" />
	<property name="messageListeners">
		<map>
			<entry key-ref="myMessageListener" value-ref="channelTopic"/>
		</map>
	</property>
</bean>
消费者 监听

@Slf4j
@Component
public class MyMessageListener implements MessageListener {
   
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel());
        String body = new String(message.getBody());
		//处理逻辑
    }
}
生产者

@Component
public class RedisService{

	@Autowired
	private StringRedisTemplate redisTemplate;

	public void sendMessage(String channel, Object message) {
		redisTemplate.convertAndSend(channel,message);
	}
}	
相关文章
相关标签/搜索