SpringBoot整合ArtemisMQ笔记
本案例是springboot2.4.2整合Apache ArtemisMQ,
发送jms信息和订阅jms消息的代码示例
pom配置
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-artemis</ artifactId> < version> 2.4.2</ version> </ dependency>
application.yml配置
spring : artemis : mode : nativehost : localhostport : 61616 user : adminpassword : 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 ;
@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 < 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 ( ) ; 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) ; } } }