Spring整合MQ(rabbit),及简单测试实列

一 .

1.万事先导包。springboot工程下,导入如下包。

<dependency>

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

<artifactId>spring-boot-starter-amqp</artifactId>

</dependency>

2.编写配置类。

@Configuration
@Slf4j
public class RabbitConfig {
    private static String NJS_EXCHANGE = "njs.in_home.topic";

    @Bean
    public Queue njsQueue() {

        log.info("njsQueue 实例化成功...");
        // 如果rabbit 里没有这个队列,会创建该队列
        return new Queue("n_js",true);
    }

    @Bean
    TopicExchange njsInHomeTopicExchange() {
        // 如果rabbit 里没有这个exchange,则会创建一个
        log.info("Topic 实例化成功...,name={}",NJS_EXCHANGE);
        return new TopicExchange(NJS_EXCHANGE,true,false);
    }

    /*
        绑定队列到 njs.in_home.topic exchange
     */
    @Bean
    Binding njsQueue2WxInHomeTopic() {
        // 绑定队列到topic. 这里的routingKey是binding key.
        log.info(" hrQueue 绑定 Topic 成功");
        return      
BindingBuilder.bind(njsQueue()).to(njsInHomeTopicExchange()).with("njs.#");
    }

}

配置完成!

二 。接下来是测试实列:

1.编写的Controller

@RestController
@RequestMapping("/mymq")
@Slf4j
public class MessageController {

    // 要和Config里的一致或和rabbitmq里的对应exchange名一样
    private String NJS_IN_HOME_TOPIC_NAME = "njs.in_home.topic";
    //注入使用RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String send(){

        log.info("start send message  .... ");
        //循环开线程,10次,每个线程发两次
        for (int i=0;i<10;i++) {
            UserJpa user=new User();
            user.setName("NJS"+i);
            user.setAge(i);
            //将对象序列化为子节数组
            byte[] bytes= SerializationUtils.serialize(user);
            //开多线程并启动
            new MyThread(bytes,rabbitTemplate,2,NJS_IN_HOME_TOPIC_NAME).start();
        }
    }

}

//我的多线程类,继承Thread
class MyThread extends Thread{
    //对象的字节数组
    private byte[] bytes=null;
    private RabbitTemplate rabbitTemplate=null;
    //发送的次数
    private int temp=0;
    private String NJS_IN_HOME_TOPIC_NAME = null;
    public MyThread(byte[] bytes,RabbitTemplate rabbitTemplate,int temp,String NJS_IN_HOME_TOPIC_NAME){
        this.bytes=bytes;
        this.rabbitTemplate=rabbitTemplate;
        this.temp=temp;
        this.NJS_IN_HOME_TOPIC_NAME=NJS_IN_HOME_TOPIC_NAME;
    }
    @Override
    public void run() {
        int k=0;
        while (k<this.temp){
            //发消息
           rabbitTemplate.convertAndSend(NJS_IN_HOME_TOPIC_NAME,"njs.wd",bytes);
           k++;
        }
    }
}

2.编写处理消息的类:

@Slf4j
@Component
//    @RabbitListener(queues = "n_js")//从哪些队列里取消息,可以写多个,写的是队列名。
@RabbitListener(queuesToDeclare = @Queue("n_js"))
public class HRMQListener {
    //对象序列化后存进N-Js.txt
    private File file=new File("F:\\N-Js.txt");
  //添加 @RabbitListener 注解来指定某方法作为消息消费的方法,例如监听某 Queue 里面的消息
    @RabbitHandler
    public void receive(byte[] bytes){
        //将字节化的对象恢复为java对象
        UserJpa userJpa= (UserJpa) SerializationUtils.deserialize(bytes);
       //调用自己写的ObjectToTxt
        ObjectTotxt(userJpa);
    }
    private void ObjectTotxt(UserJpa userJpa){
        //不存在创建N-Js.txt
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ObjectOutputStream oos = null;
        try {
            //序列化构造方法
            oos = new ObjectOutputStream(new FileOutputStream(file,true));
            //序列化实现方法
            oos.writeObject(userJpa) ;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

编写完成,启动测试!

运行结果如下,对象都存进了F:\\N-Js.txt里:

 测试结束,也可使用该方法,将对象存进数据库!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值