1. pom.xml
<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>
2. application.properties
#配置artemis的内容
spring.artemis.broker-url=tcp://localhost:61616
spring.artemis.password=
spring.artemis.user=admin
# JMS的内容:
# 默认的队列
spring.jms.template.default-destination=test00001
spring.jms.pub-sub-domain=false
3. 生产者
Send.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Send {
@Autowired
JmsTemplate jmsTemplate;
@RequestMapping("/send")
public String test02() {
// 在配置文件里指定了默认队列,所以这里不用写
jmsTemplate.convertAndSend("ssss-111");
return "Test";
}
}
4. 消费者
Accept.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
@RestController
public class Accept {
@Autowired
JmsTemplate jmsTemplate;
// 监听一个
@RequestMapping("/accept")
public String test02() throws JMSException {
Message message = jmsTemplate.receive("test00001");
String text = ((TextMessage) message).getText();
return text;
}
// 监听器自动监听test00001这个队列,不用调用
@JmsListener(destination = "test00001")
public void test(Message message) throws InterruptedException, JMSException {
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
Thread.sleep(3000);
}
}