activeMq之hello(java)
阿新 • • 發佈:2017-12-20
png cti 創建 cal config images queue idt maven
消息隊列activeMq, 節省響應時間,解決了第三方響應時間長的問題讓其他客戶可以繼續訪問,
安裝activeMq
apache-activemq-5.14.0-bin\apache-activemq-5.14.0\bin\win64\activeMq.bat
創建一個maven java project
在瀏覽器中訪問路徑 http://localhost:8161/ 登錄名admin 密碼為admin
1.pom.xml文件
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.14.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
生產任務
public static void main(String[] args) throws JMSException { //連接工廠 ConnectionFactory factory = new ActiveMQConnectionFactory(); //獲取一個連接 Connection connection = factory.createConnection(); //建立會話 Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //創建隊列化話題對象 Queue queue = session.createQueue("hello"); MessageProducer producer = session.createProducer(queue); for (int i = 0; i < 10; i++) { producer.send(session.createTextMessage("ActiveMQ"+i)); } session.commit(); }
產生了十條任務
消費(處理業務)
public static void main(String[] args) throws Exception { //連接工廠 ConnectionFactory factory = new ActiveMQConnectionFactory(); //獲取一個連接 Connection connection = factory.createConnection(); connection.start(); //建立會話 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //創建隊列化話題對象 Queue queue = session.createQueue("hello"); MessageConsumer producer = session.createConsumer(queue); while(true){ //接收消息 TextMessage receive = (TextMessage) producer.receive(); if(receive!=null){ System.out.println(receive.getText()); } } }
activeMq之hello(java)