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的编程过程很简单,概 ...
随机推荐
- ffmpeg在shell循环中只执行一次问题
最近写了一个shell脚本,发现 ffmpeg 命令只执行了一次就停了,最后找到原因: ffmpeg有时会读取标准输入流,导致命令出错,解决办法是在ffmpeg命令之后添加 #xxx ffmpeg x ...
- [POJ1284]Primitive Roots(原根性质的应用)
题目:http://poj.org/problem?id=1284 题意:就是求一个奇素数有多少个原根 分析: 使得方程a^x=1(mod m)成立的最小正整数x是φ(m),则称a是m的一个原根 然后 ...
- Vmware player 12
免费版的虚拟机Vmware,体积小.运行快速... 官方下载界面 下载地址: http://yunpan.cn/cm5smywVvqS8V 访问密码 35ac 官方下载:点击下载
- 简单的解释XSS攻击
XSS 跨站点脚本 cross site script 怎么造成攻击? 举例:有一个公共的页面,所有用户都可以访问且可以保存内容,输入的时候若输入<script>alert('I am h ...
- 1018LINUX中crontab的用法
转自http://blog.csdn.net/ethanzhao/article/details/4406017#comments 基本格式 :* * * * * command分 时 日 月 周 命 ...
- 非常实用的jquery版表单验证
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...
- 分析函数——keep(dense_rank first/last)
来源于:http://blog.itpub.net/28929558/viewspace-1182183/ 销售表:SQL> select * from criss_sales where de ...
- Java--笔记(5)
41.面向对象的五大基本原则 (1)单一职责原则(SRP) (2)开放封闭原则(OCP) (3)里氏替换原则(LSP) (4)依赖倒置原则(DIP) (5)接口隔离原则(ISP) 单一职责原则(SRP ...
- mysql 备份恢复图
http://blog.csdn.net/oldboy8/article/details/8294631
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...