1. download artemis from apache
ActiveMQhttps://activemq.apache.org/components/artemis/download/2. 解压缩到 C:/software/apache-artemis-2.30.0/
2. 进入到cmd, 执行
C:\software\apache-artemis-2.30.0\bin>artemis create C:/software/apache-artemis-2.30.0/xbroker --windows
xbroker被创建
3. 进入broker,启动
C:\software\apache-artemis-2.30.0\xbroker\bin>artemis run
4. 进入控制台
http://localhost:8161/console
启动成功, 进入创建,待续
5. 建立一个springboot project, 使用web starter和 AcitveMQ Artemis
pom.xm以下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-artemis</artifactId> </dependency> |
配置application.propertis
spring.application.name=artemisMq server.servlet.context-path=/artemisMq server.port=9090 blue.mq.in=blueQueue spring.artemis.broker-url=tcp://localhost:61616 spring.artemis.user=blue #spring.artemis.mode=native #spring.artemis.host=localhost #spring.artemis.port=61616 #spring.artemis.password=blue #spring.artemis.pool.enabled=true #spring.artemis.pool.max-connections=50 #spring.artemis.embedded.enabled=false |
创建msg sender
@Log4j2 @Service public class BlueQueueSender { @Autowired JmsTemplate jmsTemplate; @Value("${blue.mq.in}") String queueName; public void sendMsg(String msg) throws Exception { jmsTemplate.convertAndSend(queueName, msg); log.info("Msg is sent to queue[" + queueName + "] successfully!"); } } |
创建接收者
@Log4j2 @Component @EnableJms public class BlueQueueListener { @Autowired BlueQueueSender msgService; @JmsListener(destination = "${blue.mq.in}") public void onMsg(Message msg) { try { Thread.sleep(1000); String msgBody = msg.getBody(String.class); System.out.println("******************************receive msg :"+msgBody); if (StringUtils.isBlank(msgBody)) { throw new Exception("Message body is empty!"); } } catch (Exception ex) { ex.printStackTrace(); } } } |
创建一个Rest Method
@RestController @SpringBootApplication public class ArtemisMqApplication { @Autowired BlueQueueSender msgSvc; public static void main(String[] args) { SpringApplication.run(ArtemisMqApplication.class, args); } @PostMapping("/sendMsg") public String sendMsg(@RequestBody String msg) throws Exception { msgSvc.sendMsg(msg); return "success"; } } |
使用postman发送消息,可以看到Listener打印出接收到消息