jboss jms 实例
1.下载Jboss 7并配置运行环境 http://www.jboss.org/jbossas/downloads
2.增加testjms用户(用以jms链接时使用),在jboss的bin目录下运行add-user.bat,如下: guest 一定要写。
3.修改启动配置文件,使用standalone-full.xml启动jboss服务器
- $JBOSS_HOME$\bin>standalone.bat --server-config=standalone-full.xml

4.例子:修改Jboss配置文件standalone-full.xml,找到hornetq-server节点,在该节点下的jms-destinations确定含有以下配置:
- <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>
客户端项目用eclipse集成jboss的 j2ee项目。runtime是jboss 7.
public Object Receive(String filter, long timeoutReceive) throws JMSException {
System.out.println ("======enter Receive===" );
System.out.println("Creating cosumer with filter: " + filter );
MessageConsumer consumer = queuesession.createConsumer(queue, filter);
Message mrcv = null;
System.out.println("receive message with timeout="+timeoutReceive);
mrcv = consumer.receive(timeoutReceive);
if (mrcv != null) {
try {
System.out.println("RCV1: " + mrcv.getJMSCorrelationID());
System.out.println("====== consumer.close();.=====");
consumer.close();
return ((ObjectMessage) mrcv).getObject();;
} catch (MessageFormatException e) {
System.out.println("MessageFormatException: " + e.getMessage());
mrcv.acknowledge();
}
} else {
System.out.println("======receive message time out.=====");
}
consumer.close();
return null;
}
- package org.jboss.as.quickstarts.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.MessageConsumer;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- public class JMSProducer {
- private static final Logger log = Logger.getLogger(JMSProducer.class.getName());
- // Set up all the default values
- private static final String DEFAULT_MESSAGE = "Hello, World!";
- private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
- private static final String DEFAULT_DESTINATION = "jms/queue/test";
- private static final String DEFAULT_MESSAGE_COUNT = "1";
- private static final String DEFAULT_USERNAME = "testjms";
- 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;
- MessageProducer producer = null;
- Destination destination = null;
- TextMessage message = null;
- Context context = null;
- try {
- // Set up the context for the JNDI lookup
- final Properties env = new Properties();
- env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
- env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
- env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
- env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
- context = new InitialContext(env);
- // Perform the JNDI lookups
- String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
- log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
- connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
- log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
- String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
- log.info("Attempting to acquire destination \"" + destinationString + "\"");
- destination = (Destination) context.lookup(destinationString);
- log.info("Found destination \"" + destinationString + "\" in JNDI");
- // Create the JMS connection, session, producer, and consumer
- connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- producer = session.createProducer(destination);
- connection.start();
- int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
- String content = System.getProperty("message.content", DEFAULT_MESSAGE);
- log.info("Sending " + count + " messages with content: " + content);
- // Send the specified number of messages
- for (int i = 0; i < count; i++) {
- message = session.createTextMessage(content);
- producer.send(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();
- }
- // closing the connection takes care of the session, producer, and consumer
- if (connection != null) {
- connection.close();
- }
- }
- }
- }

- package org.jboss.as.quickstarts.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.MessageConsumer;
- import javax.jms.MessageProducer;
- 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());
- // Set up all the default values
- private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
- private static final String DEFAULT_DESTINATION = "jms/queue/test";
- private static final String DEFAULT_USERNAME = "testjms";
- 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 {
- // Set up the context for the JNDI lookup
- final Properties env = new Properties();
- env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
- env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
- env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
- env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
- context = new InitialContext(env);
- // Perform the JNDI lookups
- String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
- log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
- connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
- log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
- String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
- log.info("Attempting to acquire destination \"" + destinationString + "\"");
- destination = (Destination) context.lookup(destinationString);
- log.info("Found destination \"" + destinationString + "\" in JNDI");
- // Create the JMS connection, session, producer, and consumer
- connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- consumer = session.createConsumer(destination);
- connection.start();
- //等待30秒退出
- CountDownLatch latch = new CountDownLatch(1);
- // Then receive the same number of messaes that were sent
- while(message == null) {
- log.info("----receive message");
- message = (TextMessage) consumer.receive(5000);
- latch.await(1, TimeUnit.SECONDS);
- }
- log.info("====Received message with content " + message.getText());
- } catch (Exception e) {
- log.severe(e.getMessage());
- throw e;
- } finally {
- if (context != null) {
- context.close();
- }
- // closing the connection takes care of the session, producer, and consumer
- if (connection != null) {
- connection.close();
- }
- }
- }
- }

Pub/Sub demo
- package com.lym.jms;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Properties;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- public class JMSPub {
- private static final String DEFAULT_USERNAME = "testjms";
- 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";
- /**
- * @param args
- * @throws NamingException
- * @throws JMSException
- * @throws IOException
- */
- public static void main(String[] args) throws NamingException, JMSException, IOException {
- final Properties env = new Properties();
- env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
- env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
- env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
- env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
- InitialContext context = new InitialContext(env);
- // 1) lookup connection factory (local JNDI lookup, no credentials required)
- javax.jms.ConnectionFactory cf = (javax.jms.ConnectionFactory)context.lookup("jms/RemoteConnectionFactory");
- // 2) create a connection to the remote provider
- javax.jms.Connection connection = cf.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
- // 3) create session session
- boolean transacted = false;
- javax.jms.Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
- /**
- * 在standalone-full.xml中找到
- * <subsystem xmlns="urn:jboss:domain:messaging:1.1">
- * 节点下找到<jms-destinations>节点,增加
- * <jms-topic name="ServerNotificationTopic">
- <entry name="topic/ServerNotification"/>
- <entry name="java:jboss/exported/jms/topic/ServerNotification"/>
- </jms-topic>
- */
- // 4) "create" the queue/topic (using the topic name - not JNDI)
- //javax.jms.Topic topic = session.createTopic("ServerNotificationTopic");
- javax.jms.Topic topic = (javax.jms.Topic)context.lookup("jms/topic/ServerNotification");
- // 5) create producer
- //javax.jms.MessageProducer producer = session.createProducer(topic);
- Destination destination = (Destination) context.lookup("jms/topic/ServerNotification");
- javax.jms.MessageProducer producer = session.createProducer(destination);
- BufferedReader msgStream = new BufferedReader(new InputStreamReader(
- System.in));
- String line = null;
- boolean quitNow = false;
- do {
- System.out.print("Enter message (\"quit\" to quit): ");
- line = msgStream.readLine();
- if (line != null && line.trim().length() != 0) {
- // 6) create message
- TextMessage textMessage = session.createTextMessage();
- textMessage.setText(line);
- // 7) send message
- producer.send(textMessage);
- quitNow = line.equalsIgnoreCase("quit");
- }
- } while (!quitNow);
- }
- }

- package com.lym.jms;
- import java.util.Properties;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.TimeUnit;
- import javax.jms.JMSException;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- public class JMSSub {
- private static final String DEFAULT_USERNAME = "testjms";
- 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";
- /**
- * @param args
- * @throws NamingException
- * @throws JMSException
- * @throws InterruptedException
- */
- public static void main(String[] args) throws NamingException, JMSException, InterruptedException {
- final Properties env = new Properties();
- env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
- env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
- env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
- env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
- InitialContext context = new InitialContext(env);
- // javax.jms.Topic topic = (javax.jms.Topic)context.lookup("jms/topic/ServerNotification");
- // 1) lookup connection factory (local JNDI lookup, no credentials required)
- javax.jms.ConnectionFactory cf = (javax.jms.ConnectionFactory)context.lookup("jms/RemoteConnectionFactory");
- // 2) create a connection to the remote provider
- javax.jms.Connection connection = cf.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
- // 3) create session session
- boolean transacted = false;
- javax.jms.Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
- // 4) "create" the queue/topic (using the topic name - not JNDI)
- javax.jms.Topic topic = session.createTopic("ServerNotificationTopic");
- // 5) create consumer
- javax.jms.MessageConsumer consumer = session.createConsumer(topic); // messageSelector is optional
- // 6) set listener
- consumer.setMessageListener(new javax.jms.MessageListener() {
- public void onMessage(javax.jms.Message message)
- {
- try {
- TextMessage tm = (TextMessage)message;
- System.out.println("received message content: "+tm.getText().toString());
- System.out.println("getJMSDestination: "+tm.getJMSDestination());
- System.out.println("getJMSReplyTo: "+tm.getJMSReplyTo());
- System.out.println("getJMSMessageID: "+tm.getJMSMessageID());
- System.out.println("getJMSRedelivered: "+tm.getJMSRedelivered());
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- // 7) listen for messages (start the connection)
- connection.start();
- //等待30秒退出
- CountDownLatch latch = new CountDownLatch(1);
- latch.await(30, TimeUnit.SECONDS);
- }
- }

jboss jms 实例的更多相关文章
- ActionMQ5.8.0 JMS实例 手把手详细图解
出自:http://blog.csdn.net/tongjie008/article/details/40687087 ActionMQ 是开源的JMS实现 1.下载ActiveMQ 去官方网站下载: ...
- 一台机器运行多个JBoss多实例
JBossXMLJVMTomcat应用服务器 我们经常会遇到这种情况,有时候希望在同一台机器上部署若干个JBoss实例,上面运行不同的应用程序,这样的话无论由于什么原因需要对某个JBoss实例进行关 ...
- 配置JBOSS多实例
在使用Jbossserver时.非常多情况我们须要配置多个实例,以下为大家介绍JBoss里怎样配置多实例,以Jboss5.1为例介绍. 1.复制${JBOSS_HOME}\server\default ...
- 简单的JMS实例
1.首先需要一台JMS的提供者,在这里我选择使用的是apache-activemq-5.3.2这个服务器,首先下载服务器并通过bin目录下的activemq.bat启动服务器,此时可通过http:// ...
- 通过spring整合activeMQ实现jms实例
写的很详细 http://blog.csdn.net/leonardo9029/article/details/43154385
- Jboss ESB简介及开发实例
一.Jboss ESB的简介 1. 什么是ESB. ESB的全称是Enterprise Service Bus,即企业服务总线.ESB是过去消息中间件的发展,ESB采用了“总线”这样一 ...
- Linux下关闭JBoss实例
本文内容: 查看JBoss运行实例 #ps -ef|grep java 上图就是运行结果(部分结果),其中一个服务器上可能会运行多个JBOSS server实例,找到你需要看的那个. 其中 ps -e ...
- JBoss EAP应用服务器部署方法和JBoss 开发JMS消息服务小例子
一.download JBoss-EAP-6.2.0GA: http://jbossas.jboss.org/downloads JBoss Enterprise Application Platfo ...
- ActiveMQ学习总结(5)——Java消息服务JMS详解
JMS: Java消息服务(Java Message Service) JMS是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互. JMS的编程过程很简单,概 ...
随机推荐
- 正式版/免费版 Xamarin 体验与拥抱
感谢MS, 感谢老纳.终于把 Xamarin 这个磨人的小妖精给收了,在也不用向大神要破解补丁了, 终于可以光明正大的使用了!! 跟据实践, 如果你们想体验一下 .NET 开发 IOS /Androi ...
- php基础入门
一.序言 由于新公司的需要,我也就从原来的asp专向了,php的学习中.希望通过自己的学习能够尽快的熟悉了解php 二.php独特的语法特色 1.引号问题 在php中单引号和双引号的作用基本相同,但 ...
- Angular指令1
Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...
- ios8调用相机报警告: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure you(转)
我这也报了这个警告,但按他的方法并没有起作用,把写到这个地方看是否其他人用的到 错误代码:Snapshotting a view that has not been rendered results ...
- Bete冲刺第一阶段
Bete冲刺第一阶段 今日工作: github团队协作流程 web:调整dao层设计,增加新的dao组件 客户端:之前遗留的界面跳转的BUG 目前所遇问题: 第一,COCOAPODS的安装上还是有点问 ...
- 从 MySQL+MMM 到 MariaDB+Galera Cluster : 一个高可用性系统改造
很少有事情比推出高可用性(HA)系统之后便经常看到的系统崩溃更糟糕.对于我们这个Rails运行机的团队来说,这个失效的HA系统是MySQL多主复制管理器(MMM). 我们已经找寻MMM的替代品有一段时 ...
- Java--笔记(5)
41.面向对象的五大基本原则 (1)单一职责原则(SRP) (2)开放封闭原则(OCP) (3)里氏替换原则(LSP) (4)依赖倒置原则(DIP) (5)接口隔离原则(ISP) 单一职责原则(SRP ...
- iOS开发小技巧--iOS8之后的cell自动计算高度
cell高度自动计算步骤:
- mxnet(1)生成RecordIO与lst文件
(markdown是用jupypter notebook生成) mxnet为的提高IO效率, 不会直接读取图片文件, 而是先将图片列表和标签转换为RecordIO格式的二进制文件, 训练时就可以顺序读 ...
- 写chrome插件---一个优酷自动加粉丝助手
写chrome插件主要就是写js , 我们要构造界面(HTML), 以及样式(CSS), 以及chrome给我们提供的jsAPI, 主要是chrome的API, 调试的话可以使用chrome的开发者 ...