spring boot里面利用Redis发送、接收、存储消息很方便。写一个消息接收器:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class MsgReceiver {
@Autowired
StringRedisTemplate mStringRedisTemplate;
private AtomicInteger counter = new AtomicInteger();
public void receive(String message) {
System.out.println("收到消息 <" + message + "> " + counter.get());
//将收到的消息存储到Redis数据库中。
mStringRedisTemplate.opsForValue().set(UUID.randomUUID().toString(), message);
counter.incrementAndGet();
}
public int getCount() {
return counter.get();
}
}
application里面进行消息的发送:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@SpringBootApplication
public class SpringRedisMessageApplication {
private static final String CHAT_CHANNEL = "聊天";
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter adapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
container.addMessageListener(adapter, new PatternTopic(CHAT_CHANNEL));
return container;
}
@Bean
public MessageListenerAdapter messageListenerAdapter(MsgReceiver msgReceiver) {
return new MessageListenerAdapter(msgReceiver, "receive"); //"receive"对应MsgReceiver里面定义的函数名称
}
@Bean
public MsgReceiver receiver() {
return new MsgReceiver();
}
@Bean
public StringRedisTemplate template(RedisConnectionFactory factory) {
return new StringRedisTemplate(factory);
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringRedisMessageApplication.class, args);
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
MsgReceiver receiver = ctx.getBean(MsgReceiver.class);
int total = 5;
while (true) {
if (receiver.getCount() == total) {
System.out.println("已经发送条" + total + "条消息");
break;
}
System.out.println("发送消息...");
String msg = "[Message]: Hello,world! @" + System.currentTimeMillis();
template.convertAndSend(CHAT_CHANNEL, msg);
System.out.println(msg + "发送完毕");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
System.exit(0);
}
}
运行application前,先把Redis服务器启动起来。application跑起来后输出:
发送消息...
[Message]: Hello,world! @1642649316292发送完毕
收到消息 <[Message]: Hello,world! @1642649316292> 0
发送消息...
[Message]: Hello,world! @1642649316402发送完毕
收到消息 <[Message]: Hello,world! @1642649316402> 1
发送消息...
[Message]: Hello,world! @1642649316504发送完毕
收到消息 <[Message]: Hello,world! @1642649316504> 2
发送消息...
[Message]: Hello,world! @1642649316606发送完毕
收到消息 <[Message]: Hello,world! @1642649316606> 3
发送消息...
[Message]: Hello,world! @1642649316708发送完毕
收到消息 <[Message]: Hello,world! @1642649316708> 4
已经发送条5条消息
Process finished with exit code 0
用Redis的GUI数据库管理工具,可以看到消息已经存到数据库里面: