- 浏览: 704403 次
- 性别:
- 来自: 福建
文章分类
最新评论
-
羽翼的心动:
同意2楼的说法,我们公司之前一个项目用过pageoffice, ...
poi导出excel文件工具类 -
贝塔ZQ:
poi实现导出excel文件,蛮麻烦的,用pageoffice ...
poi导出excel文件工具类 -
aishiqiang:
为什么我的项目配置好证书后,每次使用jenkinst自动构建包 ...
关于使用https协议,cas认证PKIX path building failed错误解决方法 -
zhongmin2012:
谢谢分享,正在想看
AST解析java源文件相关jar包 -
mybestroy1108:
感谢分享!受益良多!
Jboss7 JMS demo
最近温习了下EJB和JMS,整理了下思路,和大家分享下P2P和Pub/Sub的demo ;JBoss 7 集成了HornetQ,JMS可以在HornetQ中间件运行,有时间在和大家分享关于HornetQ的文章。
1.下载Jboss 7并配置运行环境 http://www.jboss.org/jbossas/downloads
2.增加testjms用户(用以jms链接时使用),在jboss的bin目录下运行add-user.bat,如下:
3.修改启动配置文件,使用standalone-full.xml启动jboss服务器
4.例子:修改Jboss配置文件standalone-full.xml,找到hornetq-server节点,在该节点下的jms-destinations确定含有以下配置:
P2P demo
Pub/Sub demo
这个在jboss-client.jar 包可以找到,看你用的jboss版本了,我这里用的是JBoss AS7
已经找到了 谢谢
这个在jboss-client.jar 包可以找到,看你用的jboss版本了,我这里用的是JBoss AS7
1.下载Jboss 7并配置运行环境 http://www.jboss.org/jbossas/downloads
2.增加testjms用户(用以jms链接时使用),在jboss的bin目录下运行add-user.bat,如下:
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>
P2P demo
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); } }
评论
5 楼
mybestroy1108
2016-05-19
感谢分享!受益良多!
4 楼
摇滚轮回
2013-06-13
感谢分享!!!!!!!!!
3 楼
sfgm521
2012-09-18
lym6520 写道
sfgm521 写道
org.jboss.naming.remote.client.InitialContextFactory 请问这个类是在哪个包中?class notfound exception
这个在jboss-client.jar 包可以找到,看你用的jboss版本了,我这里用的是JBoss AS7
已经找到了 谢谢
2 楼
lym6520
2012-09-17
sfgm521 写道
org.jboss.naming.remote.client.InitialContextFactory 请问这个类是在哪个包中?class notfound exception
这个在jboss-client.jar 包可以找到,看你用的jboss版本了,我这里用的是JBoss AS7
1 楼
sfgm521
2012-09-17
org.jboss.naming.remote.client.InitialContextFactory 请问这个类是在哪个包中?class notfound exception
相关推荐
【标题】:“(转)JBOSS7.1下开发JMS(HornetQ)的示例DEMO” 在本文中,我们将深入探讨如何在JBoss Application Server 7.1 (JBoss AS 7.1)环境中开发Java消息服务(JMS)应用程序,特别是使用HornetQ作为消息...
3. **消息驱动bean(Message-Driven Beans,MDB)**:用于处理JMS(Java Message Service)消息,常用于异步处理。 4. **EJB容器**:提供事务管理、安全性、生命周期管理等服务,使得开发者可以专注于业务逻辑,而...
UserTransaction是应用程序与事务管理器交互的接口,而XAResource则代表了可以参与事务的资源,如数据库连接或JMS队列。 1. 用户事务启动:应用通过UserTransaction接口开始一个事务,如`userTransaction.begin()`...
JBOSS提供了运行和部署Java应用程序的环境,支持多种服务如JMS(Java消息服务)、JPA(Java Persistence API)和JNDI(Java命名和目录接口)。在本项目中,JBOSS作为运行EJB3组件的平台,负责调度和管理后台服务。 ...
ESB实例通常需要部署在特定的服务器环境中,如Tomcat或JBoss。源码中可能包含部署配置文件,揭示了如何在实际环境中部署和管理ESB服务。 通过深入学习和分析"ESB DEMO"提供的源码,开发者可以更好地理解和掌握ESB...
- **设置Queue**:在`jbossmq-destinations-service.xml`文件中添加新的Queue配置,该文件位于`jboss安装目录/server/default/deploy/jms/`目录下。 ```xml <mbean code="org.jboss.mq.server.jmx.Queue" name=...
#Demo 应用程序展示 J2EE 技术在 JBossAS 环境中的工作 先决条件 使用以下技术实现要求。 JBoss 7.1.1 / JBossWS / EJB 3 / JPA / JMS Java 2 SE 6 H2 数据库处于内存模式 Maven 3 演示故事 实现三个数据库表以...
《基于Red Hat JBoss Fuse的Medical IoT Demo解析与实践》 在当今的信息化时代,物联网(IoT)技术正逐步渗透到各个领域,包括医疗行业。"Medical_IoT_Demo"是一个很好的实例,展示了如何利用物联网技术在医疗场景...
2. **Message-driven Beans (MDB)**: 是一种特殊的Session Bean,主要用于处理JMS(Java Message Service)消息,它们在后台监听消息队列,当有新消息时自动触发处理。 3. **Entity Beans**: 代表持久化的业务对象...
在EJBDemo这个压缩包里,可能会包含以下文件和目录: - `pom.xml`:Maven项目配置文件,用于管理依赖和构建过程。 - `src/main/java`:源代码目录,包含了EJB组件的实现。 - `src/main/resources`:资源文件目录,...
- 在WebLogic中配置SSL,需要启用SSL并设置端口,使用KeyStore(如DemoIdentity.jks和DemoTrust.jks),在生产环境中需要导入CA证书。 7. **EJB查看**: - 通过WebLogic管理控制台的Deployment部分可以查看已发布...
- 此外,**Spring** 还提供了事务管理、DAO支持、与各种O/R Mapping框架的集成等功能,以及对J2EE组件技术的支持,例如JavaMail、EJB、JMS和WebService等。 ### 3. Hibernate 框架 - **Hibernate** 是一个成熟的全...
首先,WildFly Swarm是基于JBoss WildFly的,它简化了服务的打包和部署过程。通过将应用所需的服务和依赖打包成单一的可执行JAR,WildFly Swarm提供了高度模块化的架构,使得开发和部署变得更加高效。 在...
这是一个用于构建企业级Web应用的标准,包括一系列服务和APIs,如JDBC(Java Database Connectivity)、JMS(Java Message Service)和EJB(Enterprise JavaBeans)。Wildfly作为Java EE的实现,提供了这些服务的...
CXF框架的部署十分灵活,既可在轻量级容器如Tomcat或Spring支持的容器中运行,也可整合到JBI容器如ServiceMix、OpenESB或Petals中,甚至在J2EE应用服务器上部署,如Geronimo、JOnAS、JBoss、WebLogic及WebSphere。...
7、String 和StringBuffer的区别 JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了数值不可改变的字符串。而这个StringBuffer类提供的字符...