Generic JMS Client with EAP/ActiveMQ: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:
:<tt>[[add-user.sh]]</tt>
:<tt>[[add-user.sh]]</tt>
</blockquote>
</blockquote>
=Essential Code=
<pre>
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
p.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
InitialContext ic = new InitialContext(p);
Destination destination = (Destination)ic.lookup("jms/queue/novaordis");
ConnectionFactory cf = (ConnectionFactory)ic.lookup("/jms/RemoteConnectionFactory");
Connection c = cf.createConnection("jmsuser", "jmspassword");
c.start();
Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
Message m = session.createTextMessage("something");
producer.send(m);
c.close();
</pre>


=Code=
=Code=


{{External|https://github.com/NovaOrdis/playground/tree/master/jboss/activemq/simplest-activemq-jms-client}}
{{External|https://github.com/NovaOrdis/playground/tree/master/jboss/activemq/simplest-activemq-jms-client}}

Latest revision as of 21:21, 7 February 2017

Internal

Overview

Also see:

Generic JMS Client with HornetQ

Classpath

The classes required by the remoting and ActiveMQ client-side logic are available in $JBOSS_HOME/bin/client/jboss-client.jar so an EAP7/ActiveMQ client typically includes that JAR on its classpath.

Authentication

By default, ActiveMQ does not allow unauthenticated connections. A connection must be created for a specific user, providing appropriate credentials. The user must be added to the "ApplicationRealm", and it must have the "guest" role (it must be associated with the "guest" group when creating the user). That identity will be used to create JMS connections.

For more details on using add-user.sh, see:

add-user.sh

Essential Code

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
p.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
InitialContext ic = new InitialContext(p);

Destination destination = (Destination)ic.lookup("jms/queue/novaordis");
ConnectionFactory cf = (ConnectionFactory)ic.lookup("/jms/RemoteConnectionFactory");

Connection c = cf.createConnection("jmsuser", "jmspassword");
c.start();

Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
Message m = session.createTextMessage("something");
producer.send(m);

c.close();

Code

https://github.com/NovaOrdis/playground/tree/master/jboss/activemq/simplest-activemq-jms-client