您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
一、多种启动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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。