This examples shows how to setup and run HornetQ embedded.
HornetQ was designed to use POJOs (Plain Old Java Objects), what makes embedding HornetQ as simple as instantiating a few objects.
On this example, we only one jars (hornetq-core.jar, hornetq-jms.jar and jboss-javaee.jar).
HornetQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.
To run the example, simply type ant from this directory
In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, HornetQServer, start it and operate on JMS regularly
Configuration configuration = new ConfigurationImpl();
configuration.setEnablePersistence(false);
configuration.setSecurityEnabled(false);
configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
HornetQServer server = HornetQ.newHornetQServer(configuration);
server.start();
Queue queue = new HornetQQueue("exampleQueue");
HornetQConnectionFactory cf = new HornetQConnectionFactory (new TransportConfiguration(InVMConnectorFactory.class.getName()));
ClientSession coreSession = cf.getCoreFactory().createSession(false, false, false);
coreSession.createQueue("jms.queue.exampleQueue", "jms.queue.exampleQueue", true);
coreSession.close();
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello sent at " + new Date());
System.out.println("Sending the message.");
producer.send(message);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
System.out.println("Received TextMessage:" + messageReceived.getText());
finally
{
if (connection != null)
{
connection.close();
}
}
server.stop();