Java+redisMQ

RedisMQ

pom增加redis配置

<!-- redis -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>transmittable-thread-local</artifactId>

<version>2.12.1</version>

</dependency>

新增redisconfig

package com.xisoft.demo.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.listener.PatternTopic;

import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.concurrent.Executor;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**

* @ClassName: MyRedisMQConfig

* @author 刘文

* @date 2023/4/4 14:44

* @version 1.0

*/

@Configuration

public class MyRedisMQConfig {

private int corePoolSize = 10;

private int maxPoolSize = 20;

private int queueCapacity = 900000;

@Bean

public Executor redisMqAsyncExecutor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setThreadNamePrefix("redisMqThread-");

executor.setCorePoolSize(corePoolSize);

executor.setMaxPoolSize(maxPoolSize);

executor.setQueueCapacity(queueCapacity);

executor.initialize();

return TtlExecutors.getTtlExecutor(executor);

}

/**

* 消息监听器,使用MessageAdapter可实现自动化解码及方法代理

*

* @return

*/

@Bean

public MessageListenerAdapter listenerAdapter(MyMsgSubscriber myMsgSubscriber) {

MessageListenerAdapter adapter = new MessageListenerAdapter(myMsgSubscriber, "onMessage");

return adapter;

}

/**

* redis消息监听器容器

* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器

* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理

* @return

*/

@Bean

public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, Executor redisMqAsyncExecutor,

MessageListenerAdapter listenerAdapter) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

container.setTaskExecutor(redisMqAsyncExecutor);

container.addMessageListener(listenerAdapter, new PatternTopic("myRedisTopic"));

return container;

}

}

消费者配置

package com.xisoft.demo.config;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**

* @ClassName: MyMsgSubscriber 订阅者

* @author 刘文

* @date 2023/4/4 14:50

* @version 1.0

*/

@Component

public class MyMsgSubscriber {

private static final Logger logger = LoggerFactory.getLogger(MyMsgSubscriber.class);

@Resource

private RedisTemplate<String, Object> redisTemplate;

public void onMessage(String message, String pattern) {

logger.info("请求参数:{}", message);

try {

//将从channel拿到的message进行json转换为对象

//做业务逻辑 ...

logger.info("MyMsgSubscriber成功:{}", message);

} catch (Exception e) {

logger.error("MyMsgSubscriber失败", e);

}

}

}

使用get接口模拟生产者

package com.xisoft.demo.action;

import com.alibaba.fastjson.JSON;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.listener.ChannelTopic;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**

* @ClassName: RedisMq

* @author 刘文

* @date 2023/4/4 14:56

* @version 1.0

*/

@RestController

@RequestMapping("/redisMq")

public class RedisMq {

@Resource

private RedisTemplate<String, Object> redisTemplate;

/**

* 发布请求到redis channel,等待订阅者获取消费

*

* @return

*/

@GetMapping("/test")

public String test() {

String channel = new ChannelTopic("myRedisTopic").getTopic();

redisTemplate.convertAndSend(channel, "liuwen123");

return "";

}

}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值