Send a message to a remote JMS queue using JBoss

It looks simple, but I cannot find a simple answer.

I want to open a connection with a remote JMS broker (IP and port are known), open a session for a specific queue (name is known) and post a message in this queue.

Is there any simple Java API (possibly standard) to do this?


EDIT

Good. Now I understand that JMS is a driver specification, like JDBC, and not a communication protocol, as I thought.

Given that I am running JBoss, I still don't understand how to create a JBossConnectionFactory .


EDIT

I really gave the problem some thoughts (hmmm), and if the JMS needs to be handled in the same way as JDBC, then I need to use the client provided by my MQ implementation. Since we use SonicMQ for our broker, I decided to implement the sonic_Client.jar library provided with SonicMQ.

This works in a standalone Java application and in our JBoss service.

thanks for the help

+4
source share
3 answers

You will need to use JMS, create a QueueConnectionFactory and go from there. Exactly how you create a QueueConnectionFactory will be vendor-specific (JMS is basically a driver specification for message queues, just like JDBC for databases), but in IBM MQ it is something like this:

 MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory(); connectionFactory.setHostName(<hostname>); connectionFactory.setPort(<port>); connectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP); connectionFactory.setQueueManager(<queue manager>); connectionFactory.setChannel("SYSTEM.DEF.SVRCONN"); QueueConnection queueConnection = connectionFactory.createQueueConnection(); QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = queueSession.createQueue(<queue name>); QueueSender queueSender = session.createSender(queue); QueueReceiver queueReceiver = session.createReceiver(queue); 

EDIT (after editing the question)

The best way to access a remote queue or any other queue is to add a Queue instance to the JNDI registry. For remote queues, this is achieved using MBeans, which add a Queue instance at server startup.

Take a look at http://www.jboss.org/community/wiki/UsingWebSphereMQSeriesWithJBossASPart4 , which, although it is an example with IBM MQ, is essentially what you need to do to connect to any remote queue.

If you look at jbossmq-destinations-service.xml and org.jboss.mq.server.jmx , you will see the MBeans that you need to create for the JBoss queue.

+4
source

Here is the code we used to connect to the SonicMQ broker using the sonic_Client.jar library:

 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; public class JmsClient { public static void main(String[] args) throws JMSException { ConnectionFactory factory = new progress.message.jclient.ConnectionFactory("tcp://<host>:<port>", "<user>", "<password>"); Connection connection = factory.createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); try { MessageProducer producer = session.createProducer(session.createQueue("<queue>")); try { producer.send(session.createTextMessage("<message body>")); } finally { producer.close(); } } finally { session.close(); } } finally { connection.close(); } } } 
0
source

I actually use JBoss 4 and JNDI, not hard to use.

First of all, you need to know where JNDI works.

In my JBoss (conf \ jboss-service.xml) I have:

 <mbean code="org.jboss.naming.NamingService" name="jboss:service=Naming" xmbean-dd="resource:xmdesc/NamingService-xmbean.xml"> ... <attribute name="Port">7099</attribute> ... </mbean> 

This is important, this is the port you want to connect to.

Now you can easily connect to JNDI with this code:

 Hashtable<String, String> contextProperties = new Hashtable<String, String>(); contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); contextProperties.put(Context.PROVIDER_URL, "jnp://localhost:7099"); InitialContext initContext = new InitialContext(contextProperties); 

Now that you have the context, it is very similar to @Nick Holt, with the exception of the factory connection, you should use:

 QueueConnectionFactory connFactory = (QueueConnectionFactory) initContext.lookup("ConnectionFactory"); 

Also, you do not need to create a queue if several

 Queue queue = (Queue) initContext.lookup("queueName"); 

All the above codes were tested with JBoss 4.2.2 GA and JBossMQ (if I was right, JBossMQ replaced JBoss with 4.2.3 messages).

0
source

Source: https://habr.com/ru/post/1286678/


All Articles