1.首先是要add-user.bat,添加用戶,在jboss的bin目錄下運行add-user.bat java
照樣添加就能夠了.以後就是一路yes敲下去. 服務器
2.替換XML文件,CMD命令我用的不熟,折騰半天也沒運行起JBOSS,傷心之餘就跑去更換JBOSS的啓動使用的XML文件了.D:\software\jboss-as-7.1.3.Final\standalone\configuration文件夾下,默認使用standalone.xml,你把他備份好,而後把standalone-full.xml的名字改成standalone.xml.由於那個啥,JBOSS7默認是沒帶JMS的. session
3.修改Jboss配置文件standalone-full.xml(也就是如今的standalone.xml),找到hornetq-server節點,在該節點下的jms-destinations肯定含有如下配置. spa
<jms-destinations> <jms-queue name="testQueue"> <entry name="queue/test"/> <entry name="java:jboss/exported/jms/queue/test"/> </jms-queue> <jms-topic name="ServerNotificationTopic"> <entry name="topic/ServerNotification"/> <entry name="java:jboss/exported/jms/topic/ServerNotification"/> </jms-topic> </jms-destinations>沒就加上.
4.進入JBOSS控制檯,看到JMS就是正確的了.擦,個人standalone.bat沒法運行..才發現.不過Eclipse運行貌似一點問題沒,JBOSS TOLL插件我全裝了,汗.如圖: 插件
5.開始寫代碼,我都是寫的main方法.注意把D:\software\jboss-as-7.1.3.Final\bin\client下的jar包弄上. code
第一個是客戶端的消息生產者: server
package org.credo.jms; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import java.util.Properties; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; /** * <p>Description:JMS客戶端消息生產者 </p> */ public class JMSProducer { private static final Logger log = Logger.getLogger(JMSProducer.class.getName()); private static final String DEFAULT_MESSAGE = "the 4 message!"; // xml文件272行 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory"; // xml文件293行,初次找JBOSS的JNDI太不容易了 private static final String DEFAULT_DESTINATION = "jms/queue/test"; private static final String DEFAULT_MESSAGE_COUNT = "1"; private static final String DEFAULT_USERNAME = "lion"; private static final String DEFAULT_PASSWORD = "123456"; private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; private static final String PROVIDER_URL = "remote://localhost:4447"; public static void main(String[] args) throws Exception { Context context=null; Connection connection=null; try { // 設置上下文的JNDI查找 log.info("設置JNDI訪問環境信息也就是設置應用服務器的上下文信息!"); final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);// 該KEY的值爲初始化Context的工廠類,JNDI驅動的類名 env.put(Context.PROVIDER_URL, PROVIDER_URL);// 該KEY的值爲Context服務提供者的URL.命名服務提供者的URL env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME); env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);//應用用戶的登陸名,密碼. // 獲取到InitialContext對象. context = new InitialContext(env); log.info("初始化上下文,'JNDI驅動類名','服務提供者URL','應用用戶的帳戶','密碼'完畢."); log.info("獲取鏈接工廠!"); ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY); log.info("獲取目的地!"); Destination destination = (Destination) context.lookup(DEFAULT_DESTINATION); // 建立JMS鏈接、會話、生產者和消費者 connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); connection.start(); int count = Integer.parseInt(DEFAULT_MESSAGE_COUNT); // 發送特定數目的消息 TextMessage message = null; for (int i = 0; i < count; i++) { message = session.createTextMessage(DEFAULT_MESSAGE); producer.send(message); log.info("message:"+message); log.info("message:"+DEFAULT_MESSAGE); } // 等待30秒退出 CountDownLatch latch = new CountDownLatch(1); latch.await(30, TimeUnit.SECONDS); } catch (Exception e) { log.severe(e.getMessage()); throw e; } finally { if (context != null) { context.close(); } // 關閉鏈接負責會話,生產商和消費者 if (connection != null) { connection.close(); } } } }每次運行後如圖:
JBOSS端如圖所示: xml
每次向JBOSS的Queue發送消息成功的話,上面紅圈的數字都會+1. 對象
若是電腦重啓,JBOSS重啓,消息仍然是存在的.若是被接收了的話,消息就是消失. blog
2.客戶端消費者代碼:
package org.credo.jms; import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; 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 javax.naming.Context; import javax.naming.InitialContext; public class JMSConsumer { private static final Logger log = Logger.getLogger(JMSConsumer.class.getName()); private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory"; private static final String DEFAULT_DESTINATION = "jms/queue/test"; private static final String DEFAULT_USERNAME = "lion"; private static final String DEFAULT_PASSWORD = "123456"; private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; private static final String PROVIDER_URL = "remote://localhost:4447"; public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; MessageConsumer consumer = null; Destination destination = null; TextMessage message = null; Context context = null; try { final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY); env.put(Context.PROVIDER_URL, PROVIDER_URL); env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME); env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD); context = new InitialContext(env); connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY); destination = (Destination) context.lookup(DEFAULT_DESTINATION); connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(destination); connection.start(); // 等待30秒退出 CountDownLatch latch = new CountDownLatch(1); while (message == null) { log.info("開始從JBOSS端接收信息-----"); message = (TextMessage) consumer.receive(5000); latch.await(1, TimeUnit.SECONDS); } log.info("接收到的消息的內容:" + message.getText()); } catch (Exception e) { log.severe(e.getMessage()); throw e; } finally { if (context != null) { context.close(); } if (connection != null) { connection.close(); } } } }
而後繼續看JBOSS的控制檯,能夠發現消息減小了一條.
參考自:http://lym6520.iteye.com/blog/1600630