Tomee Remote JMS client communication issue

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Tomee Remote JMS client communication issue

sytee
Hi ,
I am looking to migrate one of my apps to Tomee but have run into an issue. We have a server side deployment and a swing based client which communicate with each other through JMS.

Following is my configuration on the server side :

    <Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
        BrokerXmlConfig = broker:(tcp://127.0.0.1:61616)
        ServerUrl = tcp://127.0.0.1:61616
    </Resource>

    <Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
        ResourceAdapter = MyJmsResourceAdapter
    </Resource>

    <Container id="MyJmsMdbContainer" ctype="MESSAGE">
        ResourceAdapter = MyJmsResourceAdapter
    </Container>

    <Resource id="MyResQueue" type="javax.jms.Queue" />


============================================
My server side code is as follows (posting relevant snippets from a servlet class):
    @Resource
    private ConnectionFactory connectionFactory;

    @Resource(name = "MyResQueue")
    private Queue queue;


private Optional<String> receiveMessages() {
        Optional<String> text = Optional.absent();
        try {

            Connection queueConnection = connectionFactory.createConnection();
            Session queueSession = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            MessageConsumer receiver = queueSession.createConsumer(queue);
            queueConnection.start();
            try {
                Message m = receiver.receive(1000);
                if (m != null && m instanceof TextMessage) {
                    TextMessage tm = (TextMessage) m;
                    text = Optional.of(tm.getText());
                    logger.debug(String.format("Received TextMessage with text '%s'.", text));
                } else {
                    logger.debug(String.format("No TextMessage received: '%s'", m));
                }
            } finally {
                queueSession.close();
                queueConnection.close();
            }
        } catch (Exception e) {
            logger.error("Receiving messages failed: " + e.getMessage(), e);
        }
        return text;
    }

    private void sendMessage(String text) {
        try {
       
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(queue);

            TextMessage testMessage = session.createTextMessage();
            testMessage.setText(text);
            testMessage.setStringProperty("aKey", "someRandomTestValue");
            producer.send(testMessage);
            logger.debug("Successfully sent message.");
        } catch (Exception e) {
            logger.error("Sending JMS message failed: "+e.getMessage(), e);
        }
    }


=========================

I have built a test remote client (running on a different JVM) from the examples mentioned here as follows :

public class SenderTest {

    @BeforeClass
    public static void configureClientResources() {
        // can be set this way or with the key Resource/<type>
        // in fact we create on client side a mini jndi tree
        // the key is the jndi name (the one used for the lookup)
        System.setProperty("aConnectionFactory", "connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://127.0.0.1:61616");
        System.setProperty("aQueue", "queue:org.apache.activemq.command.ActiveMQQueue:MyResQueue");
    }


    @Test
    public void send() throws Exception {
        final Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        final Context context = new InitialContext(properties);

        final Queue destination = (Queue) context.lookup("java:aQueue");
        assertNotNull(destination);
        assertEquals("MyResQueue", destination.getQueueName());

        final ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:aConnectionFactory");
        assertNotNull(connectionFactory);

        try {
     
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(destination);

            TextMessage testMessage = session.createTextMessage();
            testMessage.setText(new Date().toString());
            testMessage.setStringProperty("aKey", "someRandomTestValue");
            producer.send(testMessage);
            System.out.println("Successfully sent message.");
        } catch (Exception e) {
            System.out.println("Sending JMS message failed: " + e.getMessage());
        }
    }

    @Test
    public void receive() throws Exception {
        final Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        final Context context = new InitialContext(properties);

        final Queue destination = (Queue) context.lookup("java:aQueue");
        assertNotNull(destination);
        assertEquals("MyResQueue", destination.getQueueName());

        final ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:aConnectionFactory");
        assertNotNull(connectionFactory);

        try {
            String text = null;
            Connection queueConnection = connectionFactory.createConnection();
            Session queueSession = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            MessageConsumer receiver = queueSession.createConsumer(destination);
            queueConnection.start();
            try {
                Message m = receiver.receive(1000);
                if (m != null && m instanceof TextMessage) {
                    TextMessage tm = (TextMessage) m;
                    text = tm.getText();
                    System.out.println(String.format("Received TextMessage with text '%s'.", text));
                } else {
                    System.out.println(String.format("No TextMessage received: '%s'", m));
                }
            } finally {
                queueSession.close();
                queueConnection.close();
            }
        } catch (Exception e) {
            System.out.println("Receiving messages failed: " + e.getMessage());
        }
    }
}
=================================================================


With the above setup I am expecting the messages to flow from server side to client and vice-versa since I am subscribing to the same queue "MyResQueue". This is not happening. The above setup works only if I perform both the sending and receiving of messages through the servlet code or through remote client .

So in the above code if I invoke sendMessage(String text) and receiveMessages() from the servlet it works fine. If I invoke send() and receive() from SenderTest it works fine. But if I invoke sendMessage(String text) from servlet and receive from SenderTest it doesn't work.

Is there anything wrong in my configuration or am I missing something in my code ?
Any help would be appreciated.

Thanks,
S
Reply | Threaded
Open this post in threaded view
|

Re: Tomee Remote JMS client communication issue

Romain Manni-Bucau
Hi

can you check the jms config through JMX (I'm thinking to the connectors of
the broker and the destination name of the queue) to ensure it is aligned
with the code. Using all "in server" code means you share the same config
which can hide such errors.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-05-30 11:13 GMT+02:00 svaytee <[hidden email]>:

> Hi ,
> I am looking to migrate one of my apps to Tomee but have run into an issue.
> We have a server side deployment and a swing based client which communicate
> with each other through JMS.
>
> Following is my configuration on the server side :
>
>     <Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
>         BrokerXmlConfig = broker:(tcp://127.0.0.1:61616)
>         ServerUrl = tcp://127.0.0.1:61616
>     </Resource>
>
>     <Resource id="MyJmsConnectionFactory"
> type="javax.jms.ConnectionFactory">
>         ResourceAdapter = MyJmsResourceAdapter
>     </Resource>
>
>     <Container id="MyJmsMdbContainer" ctype="MESSAGE">
>         ResourceAdapter = MyJmsResourceAdapter
>     </Container>
>
>     <Resource id="MyResQueue" type="javax.jms.Queue" />
>
>
> ============================================
> My server side code is as follows (posting relevant snippets from a servlet
> class):
>     @Resource
>     private ConnectionFactory connectionFactory;
>
>     @Resource(name = "MyResQueue")
>     private Queue queue;
>
>
> private Optional<String> receiveMessages() {
>         Optional<String> text = Optional.absent();
>         try {
>
>             Connection queueConnection =
> connectionFactory.createConnection();
>             Session queueSession = queueConnection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
>
>             MessageConsumer receiver = queueSession.createConsumer(queue);
>             queueConnection.start();
>             try {
>                 Message m = receiver.receive(1000);
>                 if (m != null && m instanceof TextMessage) {
>                     TextMessage tm = (TextMessage) m;
>                     text = Optional.of(tm.getText());
>                     logger.debug(String.format("Received TextMessage with
> text '%s'.", text));
>                 } else {
>                     logger.debug(String.format("No TextMessage received:
> '%s'", m));
>                 }
>             } finally {
>                 queueSession.close();
>                 queueConnection.close();
>             }
>         } catch (Exception e) {
>             logger.error("Receiving messages failed: " + e.getMessage(),
> e);
>         }
>         return text;
>     }
>
>     private void sendMessage(String text) {
>         try {
>
>             Connection connection = connectionFactory.createConnection();
>             Session session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
>             MessageProducer producer = session.createProducer(queue);
>
>             TextMessage testMessage = session.createTextMessage();
>             testMessage.setText(text);
>             testMessage.setStringProperty("aKey", "someRandomTestValue");
>             producer.send(testMessage);
>             logger.debug("Successfully sent message.");
>         } catch (Exception e) {
>             logger.error("Sending JMS message failed: "+e.getMessage(), e);
>         }
>     }
>
>
> =========================
>
> I have built a test remote client (running on a different JVM) from the
> examples mentioned here as follows :
>
> public class SenderTest {
>
>     @BeforeClass
>     public static void configureClientResources() {
>         // can be set this way or with the key Resource/<type>
>         // in fact we create on client side a mini jndi tree
>         // the key is the jndi name (the one used for the lookup)
>         System.setProperty("aConnectionFactory",
> "connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://
> 127.0.0.1:61616");
>         System.setProperty("aQueue",
> "queue:org.apache.activemq.command.ActiveMQQueue:MyResQueue");
>     }
>
>
>     @Test
>     public void send() throws Exception {
>         final Properties properties = new Properties();
>         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.RemoteInitialContextFactory");
>         final Context context = new InitialContext(properties);
>
>         final Queue destination = (Queue) context.lookup("java:aQueue");
>         assertNotNull(destination);
>         assertEquals("MyResQueue", destination.getQueueName());
>
>         final ConnectionFactory connectionFactory = (ConnectionFactory)
> context.lookup("java:aConnectionFactory");
>         assertNotNull(connectionFactory);
>
>         try {
>
>             Connection connection = connectionFactory.createConnection();
>             Session session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
>             MessageProducer producer = session.createProducer(
> destination);
>
>             TextMessage testMessage = session.createTextMessage();
>             testMessage.setText(new Date().toString());
>             testMessage.setStringProperty("aKey", "someRandomTestValue");
>             producer.send(testMessage);
>             System.out.println("Successfully sent message.");
>         } catch (Exception e) {
>             System.out.println("Sending JMS message failed: " +
> e.getMessage());
>         }
>     }
>
>     @Test
>     public void receive() throws Exception {
>         final Properties properties = new Properties();
>         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.RemoteInitialContextFactory");
>         final Context context = new InitialContext(properties);
>
>         final Queue destination = (Queue) context.lookup("java:aQueue");
>         assertNotNull(destination);
>         assertEquals("MyResQueue", destination.getQueueName());
>
>         final ConnectionFactory connectionFactory = (ConnectionFactory)
> context.lookup("java:aConnectionFactory");
>         assertNotNull(connectionFactory);
>
>         try {
>             String text = null;
>             Connection queueConnection =
> connectionFactory.createConnection();
>             Session queueSession = queueConnection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
>
>             MessageConsumer receiver =
> queueSession.createConsumer(destination);
>             queueConnection.start();
>             try {
>                 Message m = receiver.receive(1000);
>                 if (m != null && m instanceof TextMessage) {
>                     TextMessage tm = (TextMessage) m;
>                     text = tm.getText();
>                     System.out.println(String.format("Received TextMessage
> with text '%s'.", text));
>                 } else {
>                     System.out.println(String.format("No TextMessage
> received: '%s'", m));
>                 }
>             } finally {
>                 queueSession.close();
>                 queueConnection.close();
>             }
>         } catch (Exception e) {
>             System.out.println("Receiving messages failed: " +
> e.getMessage());
>         }
>     }
> }
> =================================================================
>
>
> With the above setup I am expecting the messages to flow from server side
> to
> client and vice-versa since I am subscribing to the same queue
> "MyResQueue".
> This is not happening. The above setup works only if I perform both the
> sending and receiving of messages through the servlet code or through
> remote
> client .
>
> So in the above code if I invoke sendMessage(String text) and
> receiveMessages() from the servlet it works fine. If I invoke send() and
> receive() from SenderTest it works fine. But if I invoke sendMessage(String
> text) from servlet and receive from SenderTest it doesn't work.
>
> Is there anything wrong in my configuration or am I missing something in my
> code ?
> Any help would be appreciated.
>
> Thanks,
> S
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/Tomee-Remote-JMS-client-communication-issue-tp4681776.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Tomee Remote JMS client communication issue

sytee
Thanks for the prompt reply. Identified the issue. I had put my jms config inside WEB-INF/resources.xml contained in the server deployment. So in order to access it from remote client
had to change the following line :
System.setProperty("aQueue", "queue:org.apache.activemq.command.ActiveMQQueue:MyResQueue");
to
System.setProperty("aQueue", "queue:org.apache.activemq.command.ActiveMQQueue:tomee-jms/MyResQueue"); with "tomee-jms" being my application name .

In my previous setup, the server code was publishing to the queue "tomee-jms/MyResQueue" and the remote client to the queue "MyResQueue"

Thanks,
S