简体   繁体   中英

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. 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

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).

1.Message driven beans: In the @MessageDriven annotations it is possible to configure the bean with @ActivationConfigProperty annotations, eg:

@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? I miss something here I think..

2.For the clients I use managed beans. 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

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

In the mdb I get the queue from the client with

 @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.

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.).

lets have a look on how it can be done on Wildfly/JBoss. The server uses ActiveMQ messaging subsystem, where we can define connection factories, queues, topics etc.

<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). The default config already provides a local connection factory( InVmConnectionFactory ) and two queues - dead letter queue and Expiry queue. Lets add you new queues:
Every queue needs to have a name and at least one jndi lookup entry.

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

This defines you clientQueue queue, that should satisfy your lookup via @Resource(name = "clientQueue") .

Please note that your MDB also needs additional configuration to specify, from what queue is listens for messages.

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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