2014-10-31 06:20:28 -04:00
< html >
< head >
2014-11-19 03:44:57 -05:00
< title > ActiveMQ Embedded Example< / title >
2014-10-31 06:20:28 -04:00
< link rel = "stylesheet" type = "text/css" href = "../../common/common.css" / >
< link rel = "stylesheet" type = "text/css" href = "../../common/prettify.css" / >
< script type = "text/javascript" src = "../../common/prettify.js" > < / script >
< / head >
< body onload = "prettyPrint()" >
< h1 > Embedded Example< / h1 >
2014-11-19 03:44:57 -05:00
< p > This example shows how to setup and run ActiveMQ embedded with remote clients connecting.< / p >
< p > ActiveMQ was designed to use POJOs (Plain Old Java Objects), what makes embedding ActiveMQ as simple as instantiating a few objects.< / p >
2014-10-31 06:20:28 -04:00
2014-11-19 03:44:57 -05:00
< p > ActiveMQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.< / p >
2014-10-31 06:20:28 -04:00
< h2 > Example step-by-step< / h2 >
< p > < i > To run the example, simply type < code > mvn -Pserver< / code > from this directory to start the server and < code > mvn -Pclient< / code > to run the client example< / i > < / p >
2014-11-19 03:44:57 -05:00
< p > In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, ActiveMQServer, start it and operate on JMS regularly< / p >
2014-10-31 06:20:28 -04:00
< br / >
< ol >
< li > On EmbeddedServer: Create the Configuration, and set the properties accordingly< / li >
< pre class = "prettyprint" >
Configuration configuration = new ConfigurationImpl();
configuration.setEnablePersistence(false);
configuration.setSecurityEnabled(false);
< / pre >
< li > On EmbeddedServer: Create and start the server< / li >
< pre class = "prettyprint" >
2014-11-19 03:44:57 -05:00
ActiveMQServer server = ActiveMQ.newActiveMQServer(configuration);
2014-10-31 06:20:28 -04:00
server.start();
< / pre >
< li > As we are not using a JNDI environment we instantiate the objects directly< / li >
< pre class = "prettyprint" >
2014-11-19 03:44:57 -05:00
ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()));
2014-10-31 06:20:28 -04:00
ClientSessionFactory sf = serverLocator.createSessionFactory();
< / pre >
< li > Create a Core Queue< / li >
< pre class = "prettyprint" >
ClientSession coreSession = sf.createSession(false, false, false);
final String queueName = "queue.exampleQueue";
coreSession.createQueue(queueName, queueName, true);
coreSession.close();
< / pre >
< li > Create the session and producer< / li >
< pre class = "prettyprint" >
session = sf.createSession();
ClientProducer producer = session.createProducer(queueName);
< / pre >
< li > Create and send a Message< / li >
< pre class = "prettyprint" >
ClientMessage message = session.createMessage(false);
message.putStringProperty(propName, "Hello sent at " + new Date());
System.out.println("Sending the message.");
producer.send(message);
< / pre >
< li > Create the message consumer and start the connection< / li >
< pre class = "prettyprint" >
ClientConsumer messageConsumer = session.createConsumer(queueName);
session.start();
< / pre >
< li > Receive the message< / li >
< pre class = "prettyprint" >
ClientMessage messageReceived = messageConsumer.receive(1000);
System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));
< / pre >
< li > Be sure to close our resources!< / li >
< pre class = "prettyprint" >
if (sf != null)
{
sf.close();
}
< / pre >
< / ol >
< / body >
< / html >