Publish the so-called subscription model is released the news channel thrown in consumer consumption
Look at the code:
1 first need to configure the connection factory, this place may have been in doubt, why the need to make it a separate category, is connected with the need to use elsewhere in the plant, so the way to inject bean
@Configuration public class Redisconfig { @Value("${redis.database}") private int database; @Value("${redis.host}") private String hostName; @Value("${redis.port}") private int port; @Value("${redis.password}") private String password; @Bean(name = "redisConnectionFactory") protected RedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(hostName); redisStandaloneConfiguration.setPort(port); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));// 60s return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build()); } }
2 Configuration 2
/** * @program: operation->RedisTemplateConfig * @description: * @author: cxy * @create: 2019-12-26 17:55 **/ @Configuration public class RedisTemplateConfig { @Autowired RedisConnectionFactory redisConnectionFactory; @Bean public StringRedisTemplate redisTemplate() { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(redisConnectionFactory); return temple; } @Bean RedisMessageListenerContainer container( MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory (redisConnectionFactory);
// channel here that "cxy" channel listener is configured to be the same as the sender container.addMessageListener(listenerAdapter, new PatternTopic("cxy")); return container; } // use reflection to create a method of performing the intercepted message after, RedisReceiver class is the recipient, the recipient methods receiveMessage
@Bean MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver) {
return new MessageListenerAdapter(redisReceiver, "receiveMessage"); }
}
3RedisReceiver accepted category
@Service public class RedisReceiver { public void receiveMessage (String Message) { // This is the method of performing channel messages received after the System. OUT .println ( " Message: " + Message); } }
4 the transmission method:
@RequestMapping(value = "/q", method = RequestMethod.POST) public Result q() { redisTemplate.convertAndSend ( " Cxy " , " I am Chinese " ); return Result.success (); }
5 redis main configurations:
repeat: host: password: poolMaxIdle: 500 poolMaxTotal: 1000 poolMaxWait: 500 port: 6379 Timeout: 10 database: 0
6 result of the call:
See receive a