activemq使用
生活随笔
收集整理的這篇文章主要介紹了
activemq使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
activemq使用
ACTIVE官網
cd /usr/local/software/apache-activemq-5.15.0/bin/ ./activemq start ps -ef|grep activemq http://localhost:8161/admin admin/1234 vim jetty.xml 107 <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start"> 108 <!-- the default port number for the web console --> 109 <property name="host" value="0.0.0.0"/> 110 <property name="port" value="8161"/> 111 </bean>vim jetty-realm.properties # Defines users that can access the web (console, demo, etc.) # username: password [,rolename ...] admin: admin, admin user: user, user activemq.xml<transportConnectors><!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --><transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/><transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/><transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/><transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/><transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/></transportConnectors> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.abcd.activemq</groupId><artifactId>activemqConsumer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><properties> <activemq.version>5.11.2</activemq.version> </properties><dependencies> <dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>${activemq.version}</version></dependency> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency></dependencies></project> import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws Exception {// 第一步:建立ConnectionFactory工廠對象,需要填入用戶名、密碼、以及要連接的地址,均使用默認即可,默認端口tcp://loalhost:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin","1234","tcp://localhost:61616");// 第二步:通過ConnectionFactory工廠對象我們創建一個Connection連接,并且調用Connection的start方法開啟連接,Connection默認是關閉的Connection connection = connectionFactory.createConnection();connection.start();// 第三步:通過connection對象創建Session會話(上下文環境對象),用于接收消息,參數配置1為是否啟用事務,參數配置2為簽收模式Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);// 第四步:通過Session創建Destination對象,指的是一個客戶端用來指定生產消息目標和消費消息來源的對象,在PTP模式中,Destionation被稱為QUEUEDestination destination = session.createQueue("queue1");// 第五步:我們需要通過Session對象創建消息的發送和接收對象(生產者和消費者)MessageProducer/MessageConsumerMessageProducer messageProducer = session.createProducer(destination);// 第六步:我們可以使用MessageProducer的setDeliveryMode方法為其設置持久化特性和非持久化特性(deliveryMode)messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 第七步:最后我們使用JMS規范的TextMessage形式創建數據(通過Session對象),并用MessageProducer的send方法發送數據for(int i=0;i<5;i++) { TextMessage textMessage = session.createTextMessage("我是消息內容");textMessage.setText("我是消息內容,id為: " + i);messageProducer.send(textMessage);System.out.println("生產者: " + textMessage.getText());}if(connection!=null) { connection.close();}} } import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver {public static void main(String[] args) throws Exception {// 第一步:建立ConnectionFactory工廠對象,需要填入用戶名、密碼、以及要連接的地址,均使用默認即可,默認端口tcp://loalhost:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin","1234","tcp://localhost:61616");// 第二步:通過ConnectionFactory工廠對象我們創建一個Connection連接,并且調用Connection的start方法開啟連接,Connection默認是關閉的Connection connection = connectionFactory.createConnection();connection.start();// 第三步:通過connection對象創建Session會話(上下文環境對象),用于接收消息,參數配置1為是否啟用事務,參數配置2為簽收模式Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);// 第四步:通過Session創建Destination對象,指的是一個客戶端用來指定生產消息目標和消費消息來源的對象,在PTP模式中,Destionation被稱為QUEUEDestination destination = session.createQueue("queue1");// 第五步:我們需要通過Session對象創建消息的發送和接收對象(生產者和消費者)MessageProducer/MessageConsumerMessageConsumer consumer = session.createConsumer(destination);while(true) {TextMessage msg = (TextMessage) consumer.receive();if(msg==null)break;System.out.println("收到的內容"+msg.getText());}if(connection!=null) {connection.close();}} }?
總結
以上是生活随笔為你收集整理的activemq使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式事务的问题
- 下一篇: activemq安全机制