ejb 2.1 jboss_带有Eclipse IDE,EJB Project和JBoss 6.0 AS的JMS 1.1生产者和使用者示例

ejb 2.1 jboss

Please go through my previous posts to understand some JMS Concepts and JMS 1.1 API Basics. In this post, we are going to discuss on “How to develop JMS 1.1 Producer and Consumer Example With Eclipse IDE and JBoss 6.0 Application Server”.

请阅读我以前的文章,以了解一些JMS概念和JMS 1.1 API基础。 在本文中,我们将讨论“如何使用Eclipse IDE和JBoss 6.0 Application Server开发JMS 1.1生产者和使用者示例”。

发表简短目录 (Post Brief TOC)

  • Introduction

    介绍
  • Steps to Configure JBoss 6.0 AS In Eclipse IDE

    在Eclipse IDE中配置JBoss 6.0 AS的步骤
  • Create Eclipse EJB Project And Configure Queue

    创建Eclipse EJB项目并配置队列
  • Develop The Producer and Consumer Programs

    制定生产者和消费者计划
  • Run and Observe The results

    运行并观察结果

介绍 (Introduction)

In my previous example at “JMS 1.1 With Embedded JBoss HornetQ Server Example”, we have developed JMS 1.1 Producer and Consumer in a single program and also used Embedded Server.

在我之前的示例“带有嵌入式JBoss HornetQ Server示例的 JMS 1.1 ”中 ,我们已经在单个程序中开发了JMS 1.1生产者和使用者,并且还使用了嵌入式服务器。

Now, we are going to develop similar kind of example, but with two separate programs: one for Producer and another for Consumer. We use JBoss 6.0 Application Server as JMS Server.

现在,我们将开发类似的示例,但有两个单独的程序:一个针对生产者,另一个针对消费者。 我们使用JBoss 6.0 Application Server作为JMS Server。

JBoss AS (Application Server) is an open source application server from Red Hot for developing and deploying Enterprise Java applications, Web applications and services, and portals. By default, JBoss AS uses HornetQ as JMS Provider. HornetQ is an open source JMS Provider for building multi-protocol, embeddable, very high performance, clustered and asynchronous messaging systems.

JBoss AS(应用程序服务器)是Red Hot提供的开源应用程序服务器,用于开发和部署企业Java应用程序,Web应用程序和服务以及门户。 默认情况下,JBoss AS使用HornetQ作为JMS Provider。 HornetQ是一个开源JMS Provider,用于构建多协议,可嵌入,非常高性能的集群和异步消息传递系统。

In this post, we’re going to develop JMS 1.1 Application with JBoss AS V.6.x in Eclipse IDE.

在本文中,我们将在Eclipse IDE中使用JBoss AS V.6.x开发JMS 1.1应用程序。

在Eclipse IDE中配置JBoss 6.0 AS的步骤 (Steps to Configure JBoss 6.0 AS In Eclipse IDE)

  • Download JBoss AS from https://www.jboss.org/jbossas/downloads/ as zip file.

    从https://www.jboss.org/jbossas/downloads/下载JBoss AS压缩文件。
  • Extract jboss-6.0.0.Final.zip file into local file system.

    将jboss-6.0.0.Final.zip文件解压缩到本地文件系统中。
  • Create JBoss AS in Eclipse IDE.

    在Eclipse IDE中创建JBoss AS。
  • Click on “Create a new server” link to open “New Server” Window

    单击“创建新服务器”链接以打开“新服务器”窗口

    Select “JBoss AS 6.x” and Click on “Next” button. Select JBoss 6.x Path from your local file system. Select “Default” Configuration and click on “Finish” Button

    选择“ JBoss AS 6.x”,然后单击“下一步”按钮。 从本地文件系统中选择JBoss 6.x Path。 选择“默认”配置,然后单击“完成”按钮

    Now we can observe newly created server in Eclipse IDE

    现在我们可以在Eclipse IDE中观察新创建的服务器

  • Start JBoss 6.x AS in Eclipse IDE.

    在Eclipse IDE中启动JBoss 6.x AS。
  • Now we can observe JBoss 6.x AS is up and running

    现在我们可以观察到JBoss 6.x AS已启动并正在运行

创建Eclipse EJB项目并配置队列 (Create Eclipse EJB Project And Configure Queue)

  • To Configure HornetQ Destinations(Queues or Topics) with JBoss 6 AS, create an EJB Project in Eclipse IDE

    要使用JBoss 6 AS配置HornetQ目标(队列或主题),请在Eclipse IDE中创建一个EJB项目。
  • Provide Project name “HornetQConfigApp” and select Target runtime “JBoss 6.x Runtime”

    提供项目名称“ HornetQConfigApp”,然后选择目标运行时“ JBoss 6.x Runtime”

    Click on Finish Button. Now Project structure looks like

    单击完成按钮。 现在项目结构看起来像

    Create “hornetq-jms.xml” file at “ejbModule\METAINF” directory to add new Queue

    在“ ejbModule \ METAINF”目录下创建“ hornetq-jms.xml”文件以添加新队列

    hornetq-jms.xml – It is used to configure JMS Destinations (Queues and Topics) for HornetQ JMS Provider

    hornetq-jms.xml –用于为HornetQ JMS Provider配置JMS目标(队列和主题)

<configuration xmlns="urn:hornetq" 
		xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
	<queuename="JDQueue">
		<entryname="queue/JDQueue"/>
	</queue>
</configuration>
  • Deploy this Queue configuration into JBoss 6.x AS

    将此队列配置部署到JBoss 6.x AS中
  • Right click on Server and select “Add and Remove” option

    右键单击服务器,然后选择“添加和删除”选项

    Select our application and click on “Add>” button then “Finish”

    选择我们的应用程序,然后单击“添加>”按钮,然后单击“完成”

    Now our required Queue “jms/JDQueue” is created in JBoss 6 AS – HornetQ Message Broker successfully.

    现在,我们已在JBoss 6 AS – HornetQ Message Broker中成功创建了所需的队列“ jms / JDQueue”。

    制定生产者和消费者计划 (Develop The Producer and Consumer Programs)

    • Create new Java Project to develop JMS Publisher and Consumer applications

      创建新的Java项目以开发JMS Publisher和Consumer应用程序
    • Create JBossJMSPublisher program

      创建JBossJMSPublisher程序

    import javax.jms.*;
    import javax.naming.*;
    public class JBossJMSPublisher {
    	public static void main(String[] args) throws Exception{
    		Context context = new InitialContext();
    		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("/ConnectionFactory");		
    		Destination queue = (Destination)context.lookup("queue/JDQueue");
    		Connection connection = connectionFactory.createConnection();
    		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		MessageProducer producer = session.createProducer(queue);
    		TextMessage sntMessage =session.createTextMessage("JD Sample JMS Queue Message");
    		producer.send(sntMessage);
    		System.out.println("Message Sent successfully.");
    		connection.close();
    	}
    }

    Create JMS Consumer Program

    创建JMS消费者计划

    import javax.jms.*;
    import javax.naming.*;
    public class JBossJMSConsumer {
    	public static void main(String[] args) throws Exception{
    		Context context = new InitialContext();
    		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("/ConnectionFactory");		
    		Destination queue = (Destination)context.lookup("queue/JDQueue");
    		Connection connection = connectionFactory.createConnection();
    		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		MessageConsumer consumer = session.createConsumer(queue);
    		connection.start();
    		TextMessage rcvMessage = (TextMessage) consumer.receive();
    		System.out.println("Received Msg = " + rcvMessage.getText());
    		connection.close();
    	}
    }
  • Define JBoss AS JNDI in jndi.properties at “src” folder

    在“ src”文件夹的jndi.properties中定义JBoss AS JNDI
  • jndi.properties

    jndi.properties

    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
    java.naming.provider.url=jnp://localhost:1099
  • Add required jars to Project Build path

    将所需的jar添加到Project Build路径
  • Use “Add Library” button to add JBoss 6.x Library

    使用“添加库”按钮添加JBoss 6.x库

  • Final project looks like

    最终项目看起来像
  • 运行并观察结果 (Run and Observe The results)

    • Now test JMS application

      现在测试JMS应用程序
    • Run JMS Producer program to send messages to JBoss 6.x AS HornetQ Queue

      运行JMS Producer程序以将消息发送到JBoss 6.x AS HornetQ Queue

      Run JMS Consumer program and observe the message

      运行JMS Consumer程序并观察消息

    That’s it all about developing Simple JMS API 1.1 Producer and Consumer application with JBoss 6.0 Server. We will discuss JMS API 2.0 Producer and Consumer Example in my coming posts.

    这就是使用JBoss 6.0 Server开发Simple JMS API 1.1 Producer和Consumer应用程序的全部内容。 我们将在我的后续文章中讨论JMS API 2.0生产者和使用者示例。

    Please drop me a comment if you like my post or have any issues/suggestions.

    如果您喜欢我的帖子或有任何问题/建议,请给我评论。

    翻译自: https://www.journaldev.com/9906/jms1-1-with-eclipse-and-jboss6-example

    ejb 2.1 jboss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值