activeMq之hello(java)

來源:互聯網
上載者:User

標籤:commit   XML   pil   4.0   建立   while   gpo   on()   alt   

訊息佇列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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.