至关于一个ActiveMQ服务实例java
Broker其实就是实现了用代码的形式启动了ActiveMQ将MQ嵌入到java代码中,以便随时用随时启动,在用的时候再去启动这样能节约资源,也保证了可靠性。spring
一、 先将ActiveMQ根目录下conf文件夹中的activemq.xml复制一份并重命名为activemq02.xmlapache
命令(cp activemq.xml activemq02.xml)json
二、启动activemq02.xml,默认启动的是activemq.xmlspringboot
命令(./activemq start xbean:file:/usr/local/activeMQ/apache-activemq-5.15.11/conf/activemq02.xml)服务器
用ActiveMQ Broker做为独立的消息服务器来构建java应用。ActiveMQ也支持在虚拟机中通讯,基于嵌入式的broker,可以无缝的集成其余java应用session
一、pom.xml中引入包tcp
<!--activemq--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.9</version> </dependency> <!--fastjson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency>
二、broker代码 url
import org.apache.activemq.broker.BrokerService; /** * @ProjectName: springbootActiveMQ * @Package: cn.**.test * @Author: huat * @Date: 2020/1/10 16:04 * @Version: 1.0 */ public class EmbedBroker { public static void main(String[] args) throws Exception { //ActiveMQ也支持在虚拟机中通讯,嵌入broker BrokerService brokerService=new BrokerService(); //将activeMQ嵌入到java程序中 brokerService.setUseJmx(true); //如今是将activeMQ嵌入到java程序中,因此使用本机 brokerService.addConnector("tcp://127.0.0.1:61616"); //启动程序 brokerService.start(); } }
三、队列生产者代码 spa
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * @ProjectName: springbootActiveMQ * @Package: cn.**.test * @Author: huat * @Date: 2020/1/2 17:04 * @Version: 1.0 */ public class ActiveMQTest { //url路径 private static final String ACTRIVE_URL="tcp://127.0.0.1:61616"; //队列名称 private static final String QUEUE_NAME="queue01"; public static void main(String[] args) { //一、建立链接工厂 //若是帐号密码没有修改的话,帐号密码默认均为admin ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL); try { //二、经过链接工厂获取链接 Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); //三、建立session会话 //里面会有两个参数,第一个为事物,第二个是签收 Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //四、建立目的地(具体是队列仍是主题),这里是建立队列 Queue queue=session.createQueue(QUEUE_NAME); //五、建立消息生产者,队列模式 MessageProducer messageProducer = session.createProducer(queue); //六、经过messageProducer生产三条消息发送到MQ消息队列中 for (int i=0;i<3;i++){ //七、建立消息 TextMessage textMessage = session.createTextMessage("msg----->" + i);//建立一个文本消息 //八、经过messageProducer发送给mq messageProducer.send(textMessage); //九、数据非持久化 messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT); } messageProducer.close(); session.commit(); session.close(); connection.close(); System.out.println("消息发送成功"); } catch (JMSException e) { e.printStackTrace(); } } }
三、队列消费者代码
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * @ProjectName: springbootActiveMQ * @Package: cn.**.test * @Author: huat * @Date: 2020/1/3 8:47 * @Version: 1.0 */ public class ActiveMQConsumer { //url路径 private static final String ACTRIVE_URL="tcp://127.0.0.1:61616"; //队列名称 private static final String QUEUE_NAME="queue01"; public static void main(String[] args) { //一、建立链接工厂 //若是帐号密码没有修改的话,帐号密码默认均为admin ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL); //若是帐号密码修改的话 //第一个参数为帐号,第二个为密码,第三个为请求的url //ActiveMQConnectionFactory activeMQConnectionFactory1=new ActiveMQConnectionFactory("admin","admin",ACTRIVE_URL); try { //二、经过链接工厂获取链接 Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); //三、建立session会话 //里面会有两个参数,第一个为事物,第二个是签收 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //四、这里接受的queue的名称要和发送者的一致 Queue queue = session.createQueue(QUEUE_NAME); //五、建立消费者 MessageConsumer consumer = session.createConsumer(queue); //六、经过监听的方式消费消息 while(true){ //MessageConsumer 调用的receive方法为同步调用,在消息到达以前一直阻塞线程 //用什么格式发送,这里就用什么格式接受 //receive等待消息,不限制时间 TextMessage message=(TextMessage)consumer.receive(); //receive带参数等待消息,限制时间,单位毫秒 //TextMessage message=(TextMessage)consumer.receive(4000L); if(null != message){ System.out.println("接受的消息为------>"+message.getText()); }else{ break; } } //七、闭资源 consumer.close(); session.close(); connection.close(); }catch (Exception e){ e.printStackTrace(); } } }