SpringBoot整合ArtemisMQ笔记

SpringBoot整合ArtemisMQ笔记

本案例是springboot2.4.2整合Apache ArtemisMQ,
发送jms信息和订阅jms消息的代码示例

pom配置

 <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-artemisartifactId>
            <version>2.4.2version>
        dependency>

application.yml配置

spring:
  artemis:
    mode: native
    host: localhost
    port: 61616
    user: admin
    password: admin

配置使用EnableJms注解

@EnableScheduling
@EnableJms
@SpringBootApplication
public class App {
   public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

发送jms消息

package cn.demo.jms.send;

import cn.demo.IdGenerator;
import cn.demo.TestObj;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * jms 发送消息类型
 * TextMessage  字符串消息
 * ObjectMessage  对象消息
 * MapMessage  键值对消息
 * BytesMessage  字节流 消息
 * 
 */
@Component
@Slf4j
public class JmsSendTxtSchedule {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private IdGenerator idGenerator;


    @Scheduled(cron = "*/1 * * * * ?")
    public void t1(){
        //发送 字符串消息
        String msg = IdUtil.fastSimpleUUID()+":"+System.currentTimeMillis();
        log.info("t1 pre send msg:{}",msg);
        jmsTemplate.convertAndSend("queue.sample",msg);

    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void t2(){
        //发送 对象消息
        TestObj obj = new TestObj();
        obj.setId(idGenerator.snowflakeId());
        obj.setTs(System.currentTimeMillis());
        obj.setK1(IdUtil.fastSimpleUUID());
        obj.setK2("t2send");
        log.info("t2 pre send msg:{}",obj.toString());
        jmsTemplate.convertAndSend("queue.testobj",obj);
    }

    @Scheduled(cron = "*/3 * * * * ?")
    public void  t3(){
	    //发送 Map键值对消息
        Map<String,Object> msg = new HashMap<>();
        msg.put("k1","t3");
        msg.put("k2",System.currentTimeMillis());
        jmsTemplate.convertAndSend("queue.testmap",msg);
    }

    @Scheduled(cron = "*/2 * * * * ?")
    public void t4(){
        String str = "t4 --- this is a test msg"+System.currentTimeMillis();
        byte[] bytes = str.getBytes();
        //发送 字节流byte[] 消息
        jmsTemplate.convertAndSend("queue.testbytes",bytes);

    }

}

订阅jms消息

@Slf4j
@Component
public class SubQueue {

    @JmsListener(destination = "queue.testbytes")
    public void handleBytes(BytesMessage bytesMessage) throws JMSException {
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = bytesMessage.readBytes(b)) != -1) {
            String s = new String(b, 0, len);
            log.info("订阅到的1024字节的字符串:{}",s);
        }
    }


    @JmsListener(destination = "queue.testmap")
    public void handleMap(MapMessage map) {
        try {
            String k1 = map.getString("k1");
            long k2 = map.getLong("k2");
            log.info("监听到queue.testmap里的键值对k1:{}", k1);
            log.info("监听到queue.testmap里的键值对k2:{}", k2);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @JmsListener(destination = "queue.sample")
    public void handleMessage(TextMessage message) {
        try {
            String text = message.getText();
            log.info("监听到queue.sample里的信息:{}", text);

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @JmsListener(destination = "queue.testobj")
    public void handleObjMsg(ObjectMessage message) {
        try {
            TestObj object = (TestObj) message.getObject();
            log.info("监听到queue.testobj里的信息:{}", object);
        } catch (JMSException e) {
            e.printStackTrace();
            log.error("ObjectMessage转换失败!", e);
        }
    }

}

你可能感兴趣的:(SpringBoot技术笔记,MQ消息队列,spring,boot,笔记,后端,artemis,mq,jms,消息队列)