您好,登錄后才能下訂單哦!
一、多種啟動Broker的方法
二、單獨應用的開發
//1:Broker Service啟動broker,示例如下
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
//2:BrokerFactory 啟動broker,示例如下:
String uri = "properties:broker.properties";
BrokerService broker1 = BrokerFactory.createBroker(new URI(uri));
broker1.addConnector("tcp://localhost:61616");
broker1.start();
//3:broker.properties的內容如下:
useJms=true
persistent=false
brokerName=Cheese
三、結合spring boot的開發
參考:https://blog.csdn.net/liuchuanhong1/article/details/54603546
配置pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
#如果此處設置為true,需要加如下的依賴包,否則會自動配置失敗,報JmsMessagingTemplate注入失敗
spring.activemq.pool.enabled=false
生產者
@Service
public class Producer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝
private JmsMessagingTemplate jmsTemplate;
// 發送消息,destination是發送到的隊列,message是待發送的消息
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
}
@Component
public class Consumer {
使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
@JmsListener(destination = "springboot-activemq")
public void receiveQueue(String text) {
System.out.println("Consumer收到的報文為:"+text);
}
}
測試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootJmsApplicationTests {
@Autowired
private Producer producer;
@Test
public void contextLoads() {
Destination destination = new ActiveMQQueue("springboot-activemq");
for (int i = 0; i < 10; i++) {
producer.sendMessage(destination,"this message is " + i);
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。