网站建设的意义怎么写,北京网站建设 乐云seo,wordpress 模板 学校,专业移动网站建设tomee我记得J2EE #xff08;1.3和1.4#xff09;的过去#xff0c;使用JMS启动项目非常困难。 您需要安装JMS 代理 #xff0c;创建主题或队列 #xff0c;最后使用服务器配置文件和JNDI开始自己的战斗。 感谢JavaEE 6及其它#xff0c;使用JMS确实非常简单。 但是1.3和1.4的过去使用JMS启动项目非常困难。 您需要安装JMS 代理 创建主题或队列 最后使用服务器配置文件和JNDI开始自己的战斗。 感谢JavaEE 6及其它使用JMS确实非常简单。 但是使用Apache TomEE更加容易上手。 在本文中我们将了解如何创建和测试一个简单的应用程序该应用程序使用Apache TomEE向JMS队列发送消息或从JMS队列接收消息。 Apache TomEE使用Apache Active MQ作为JMS提供程序。 在此示例中您不需要下载或安装任何东西因为所有元素都将作为Maven依赖项提供但是如果您计划并且应该使用Apache TomEE服务器则需要下载Apache TomEE plus或Apache TomEE plume。 您可以在http://tomee.apache.org/comparison.html中了解有关Apache TomEE风味的更多信息。 依存关系 首先要做的是添加javaee-api作为提供的依赖关系并添加junit和openejb-core作为测试依赖关系。 请注意添加了openejb-core依赖项以使其具有运行时来执行测试我们将在测试部分中对其进行深入了解。 dependenciesdependencygroupIdorg.apache.openejb/groupIdartifactIdjavaee-api/artifactIdversion6.0-6/versionscopeprovided/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependencydependencygroupIdorg.apache.openejb/groupIdartifactIdopenejb-core/artifactIdversion4.7.1/versionscopetest/scope/dependency
/dependencies商业代码 下一步是创建负责发送消息和从JMS 队列接收消息的业务代码。 它还包含一种从队列接收消息的方法。 对于此示例我们将使用无状态 EJB 。 import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;Stateless
public class Messages {//Standard Resource annotation is used to inject the ConnectionFactory. //If no name is provided using lookup or name attribute, //the fully qualified name of the class with an slash (/) and the name of the attribute is used. //In this example: java:comp/env/org.superbiz.jms.Messages/connectionFactory.Resource private ConnectionFactory connectionFactory;//Standard Resource annotation is used to inject the Queue. //If no name is provided using lookup or name attribute, //the fully qualified name of the class with an slash (/) and the name of the attribute is used. //In this example: java:comp/env/org.superbiz.injection.jms.Messages/chatQueue.Resource private Queue chatQueue;public void sendMessage(String text) throws JMSException {Connection connection null;Session session null;try {connection connectionFactory.createConnection();//Connection is get from ConnectionFactory instance and it is started.connection.start(); //Creates a session to created connection.session connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //Creates a MessageProducer from Session to the Queue.MessageProducer producer session.createProducer(chatQueue);producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); TextMessage message session.createTextMessage(text);//Tells the producer to send the messageproducer.send(message); } finally {if (session ! null) session.close(); if (connection ! null) connection.close();}}public String receiveMessage() throws JMSException {Connection connection null;Session session null;MessageConsumer consumer null;try {connection connectionFactory.createConnection();connection.start();session connection.createSession(false, Session.AUTO_ACKNOWLEDGE);consumer session.createConsumer(chatQueue); //Waits for a message with timeout. Note that because a TextMessage is sent, the receive method expects a TextMessage too.TextMessage message (TextMessage) consumer.receive(1000); return message.getText(); } finally {if (consumer ! null) consumer.close();if (session ! null) session.close();if (connection ! null) connection.close();}}
} Messages类的最重要部分是注意注入ConnectionFactory和 在代码内将实例排队 。 您只需要使用Resource批注容器将为您完成其余工作。 最后请注意由于我们尚未使用name或lookup属性来设置名称因此该字段的名称将用作资源名称。 测试 最后我们可以编写一个测试来断言使用JMS队列发送和接收消息。 例如我们可以使用Arquilian编写测试但是由于这种情况由于简单起见我们将使用嵌入式OpenEJB实例来部署JMS示例并运行测试。 public class MessagesTest {//Messages EJB is injected.EJBprivate Messages messages;Beforepublic void setUp() throws Exception {Properties p new Properties();//Embedded OpenEJB container is started.//And current test added inside created container//So we can use javaee annotations insideEJBContainer.createEJBContainer(p).getContext().bind(inject, this); }Testpublic void shouldSendAndReceiveMessages() throws Exception {//Three messages are sent.messages.sendMessage(Hello World!); messages.sendMessage(How are you?);messages.sendMessage(Still spinning?);//Three messages are received.assertThat(messages.receiveMessage(), is(Hello World!)); assertThat(messages.receiveMessage(), is(How are you?));assertThat(messages.receiveMessage(), is(Still spinning?));}} 请注意测试确实非常简单明了您只需要以编程方式启动EJB容器并将当前测试绑定到其中这样我们就可以在测试中使用JavaEE批注。 其余的是一个简单的JUnit测试。 而且如果您运行测试您将收到绿色的子弹。 但是等等您可能想知道JMS 代理及其配置在哪里 ConnectionFactory和JMS 队列的定义在哪里 这就是OpenEJB 和Apache TomEE 发挥作用的地方。 在这种情况下 OpenEJB 和Apache TomEE 将以嵌入式模式使用Apache Active MQ 因此您无需在计算机上安装Apache Active MQ即可运行测试。 此外 Apache TomEE将为您创建所有必需的资源。 例如它会创建一个连接工厂和一个队列为你使用默认参数和预期的名称org.superbiz.Messages / connectionFactory的用于连接工厂和org.superbiz.Messages / chatQueue的队列 所以你不必担心到在测试阶段配置JMS 。 Apache TomEE足够聪明可以为您创建和配置它们。 您可以通过阅读下一条日志消息来检查控制台输出以了解资源是自动创建的 INFO自动创建资源 。 Jan 10, 2015 10:32:48 AM org.apache.openejb.config.AutoConfig processResourceRef
INFO: Auto-linking resource-ref java:comp/env/org.superbiz.Messages/connectionFactory in bean Messages to Resource(idDefault JMS Connection Factory)
Jan 10, 2015 10:32:48 AM org.apache.openejb.config.ConfigurationFactory configureService
INFO: Configuring Service(idorg.superbiz.Messages/chatQueue, typeResource, provider-idDefault Queue)
Jan 10, 2015 10:32:48 AM org.apache.openejb.config.AutoConfig logAutoCreateResource
INFO: Auto-creating a Resource with id org.superbiz.Messages/chatQueue of type javax.jms.Queue for Messages.
Jan 10, 2015 10:32:48 AM org.apache.openejb.assembler.classic.Assembler createRecipe
INFO: Creating Resource(idorg.superbiz.Messages/chatQueue)
Jan 10, 2015 10:32:48 AM org.apache.openejb.config.AutoConfig processResourceEnvRef
INFO: Auto-linking resource-env-ref java:comp/env/org.superbiz.Messages/chatQueue in bean Messages to Resource(idorg.superbiz.Messages/chatQueue)
Jan 10, 2015 10:32:48 AM org.apache.openejb.config.ConfigurationFactory configureService
INFO: Configuring Service(idDefault Managed Container, typeContainer, provider-idDefault Managed Container)
Jan 10, 2015 10:32:48 AM org.apache.openejb.config.AutoConfig createContainer
INFO: Auto-creating a container for bean javaee.MessagesTest: Container(typeMANAGED, idDefault Managed Container) 就这样借助Java EE和TomEE JMS真的非常容易上手 。 在下一篇文章中我们将看到如何使用消息驱动Bean MDB进行相同的操作。 翻译自: https://www.javacodegeeks.com/2015/01/apache-tomee-jms-it-has-never-been-so-easy.htmltomee