java開發JMS for Jboss4

來源:互聯網
上載者:User

針對好多朋友,想熟悉JMS相關開發知識,網上例子寫的也比較亂,特手動編寫了一個例子和伺服器配置,希望大家喜歡,支援我


1)  jboss配置Queue訊息佇列

     開啟jboss安裝目錄,找到server/default/deploy/jms下找到檔案jbossmq-destinations-service.xml檔案

     增加配置代碼如下:

       <mbean code="org.jboss.mq.server.jmx.Queue" 
                name="jboss.mq.destination:service=Queue,name=testQueue">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>

   啟動jboss在jmx-console下,即可查詢發布testQueue隊列成功的訊息,很簡單

2)開發發送用戶端,拷貝jboss目錄下client檔案夾下的jbossall-client.jar到項目lib下,編寫代碼如下

 import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JmsSender {
public static void main(String[] args) throws JMSException, NamingException {
 
 Properties env = new Properties();
         env.put(Context.PROVIDER_URL, "127.0.0.1:1099");
         env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
         env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
         Context context = new InitialContext(env);
         
         QueueConnectionFactory factory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
         QueueConnection queueConnection = factory.createQueueConnection();  
         QueueSession queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);   
         Queue queue = (Queue) context.lookup("queue/testQueue"); 
         TextMessage message = queueSession.createTextMessage();  
         message.setText("ddddd"); 
         QueueSender queueSender = queueSession.createSender(queue);
         queueSender.send(queue, message);
         System.out.println("-------------");
  }
}

開發完畢,很簡單

3)接收訊息端開啟如下

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsReader {
public static void main(String[] args) throws JMSException, NamingException {
Properties env = new Properties();
         env.put(Context.PROVIDER_URL, "localhost:1099");
         env.put(Context.INITIAL_CONTEXT_FACTORY,
                 "org.jnp.interfaces.NamingContextFactory");
         env.put(Context.URL_PKG_PREFIXES,
                 "org.jboss.naming:org.jnp.interfaces");
         /*
          * 利用參數串連好伺服器
          */
         InitialContext ic = new InitialContext(env);
         QueueConnection cs = ((QueueConnectionFactory) ic
                 .lookup("ConnectionFactory")).createQueueConnection();
         /*
          * 配置並串連相應的queue
          */
         Queue q = (Queue) ic.lookup("queue/testQueue");
         QueueSession qss = cs.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
         QueueReceiver qr = qss.createReceiver(q);
          /*
          * 啟動連結
          */
          cs.start();
      
         /*
          * 開始接受,然後處理接收到的 message
          */
         TextMessage message=(TextMessage)qr.receive();
        System.out.println(message.getText());
             
        cs.close();  
}

}


       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.