简体   繁体   English

具有JMS和MDB的JavaEE消息传递系统

[英]JavaEE- messaging system with JMS and MDB

I am pretty new to JavaEE and I try to build a messaging system using jms and mdb. 我对JavaEE相当陌生,我尝试使用jms和mdb构建消息系统。 I want to do the following: - a server for handling messages which is realized by a mdb - different clients who should communicate - clients should just communicate over the server and not directly 我要执行以下操作:-用于处理由mdb实现的消息的服务器-应该进行通信的不同客户端-客户端应该仅通过服务器进行通信,而不是直接进行通信

So nothing special. 所以没什么特别的。 I've read a lot in the documentations and tutorials of oracle and I am still confused about some things (although it is working). 我已经在oracle的文档和教程中阅读了很多东西,但我对某些事情仍然感到困惑(尽管它正在工作)。

1.Message driven beans: In the @MessageDriven annotations it is possible to configure the bean with @ActivationConfigProperty annotations, eg: 1.消息驱动的bean:在@MessageDriven批注中,可以使用@ActivationConfigProperty批注配置bean,例如:

@MessageDriven(mappedName = "myBean", activationConfig = {
        @ActivationConfigProperty(propertyName = "messageSelector",
                propertyValue = "requestType = 'reqA' OR requestType = 'reqB'"),
        @ActivationConfigProperty(propertyName = "destinationType",
                propertyValue = "javax.jms.Queue")})

But how do I "create" the queue for this bean? 但是,如何为该bean“创建”队列? I miss something here I think.. 我想念这里的东西。

2.For the clients I use managed beans. 2.对于客户,我使用托管bean。 Clients are able to send and receive, therefore they need a queue or a topic. 客户能够发送和接收,因此他们需要队列或主题。 I create the queue in the clients with 我在客户端创建队列

@Resource(name = "clientQueue")
private Queue clientQueue;

and get the queue from the mdb with 并从mdb获取队列

@Resource(mappedName = "serverBean")
private Queue serverQueue;

In the mdb I get the queue from the client with 在mdb中,我从客户端获取队列

 @Resource(name = "clientQueue")
 private Queue clientQueue;

Is that correct or is there a better solution? 那是正确的还是有更好的解决方案?

I know that this is very basic but I am confused because eg the tutorial form apache tomcat has some differences to the tutorial from oracle which I do not get. 我知道这是非常基础的,但是我很困惑,因为例如apache tomcat的教程形式与oracle的教程有些区别,我没有得到。

I confused myself a bit so any clarification would be very nice! 我有些困惑,所以任何澄清都很好!

The queues you are referring to, exist outside your application and it is up to your server to provide a mechanism to access them. 您要引用的队列存在于应用程序外部,由服务器决定是否提供访问它们的机制。 Simplest scenario is that your queues are defined by your server, making them easily available to all apps running there. 最简单的情况是,队列是由服务器定义的,从而使队列可以轻松地供运行于此的所有应用程序使用。 The actual definition is provider specific(ie different on jboss, payara, etc.). 实际定义是特定于提供程序的(即,在jboss,payara等上有所不同)。

lets have a look on how it can be done on Wildfly/JBoss. 让我们来看看如何在Wildfly / JBoss上完成它。 The server uses ActiveMQ messaging subsystem, where we can define connection factories, queues, topics etc. 服务器使用ActiveMQ消息子系统,我们可以在其中定义连接工厂,队列,主题等。

<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
            <server name="default">
                <security-setting name="#">
                    <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
                </security-setting>
                <address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
                <http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
                <http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
                    <param name="batch-delay" value="50"/>
                </http-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
                <http-acceptor name="http-acceptor" http-listener="default"/>
                <http-acceptor name="http-acceptor-throughput" http-listener="default">
                    <param name="batch-delay" value="50"/>
                    <param name="direct-deliver" value="false"/>
                </http-acceptor>
                <in-vm-acceptor name="in-vm" server-id="0"/>
                <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
                <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
                <connection-factory name="InVmConnectionFactory" connectors="in-vm" entries="java:/ConnectionFactory"/>
                <connection-factory name="RemoteConnectionFactory" connectors="http-connector" entries="java:jboss/exported/jms/RemoteConnectionFactory"/>
                <pooled-connection-factory name="activemq-ra" transaction="xa" connectors="in-vm" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory"/>
            </server>
        </subsystem> 

This is the default configuration on Wildfly 10(you have to use full profile, or manually add messaging-activemq subsystem to your profile). 这是Wildfly 10上的默认配置(您必须使用完整的配置文件,或手动向您的配置文件添加messaging-activemq子系统)。 The default config already provides a local connection factory( InVmConnectionFactory ) and two queues - dead letter queue and Expiry queue. 默认配置已经提供了本地连接工厂( InVmConnectionFactory )和两个队列-死信队列和到期队列。 Lets add you new queues: 让我们添加新队列:
Every queue needs to have a name and at least one jndi lookup entry. 每个队列都需要有一个名称和至少一个jndi查找条目。

<jms-queue name="ClientQueue" entries="java:/jms/queue/clientQueue"/>

This defines you clientQueue queue, that should satisfy your lookup via @Resource(name = "clientQueue") . 这定义了clientQueue队列,该队列应该通过@Resource(name = "clientQueue")满足您的查询。

Please note that your MDB also needs additional configuration to specify, from what queue is listens for messages. 请注意,您的MDB还需要其他配置来指定从哪个队列监听消息。

@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/clientQueue")

I recommend, that you start with one of the simpler examples provided by your server vendor. 我建议您从服务器供应商提供的简单示例之一开始。 There are some nice example here and here . 这里这里都有一些很好的例子。
Happy hacking. 骇客入侵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM