Tuesday, October 22, 2013

HornetQ 2.0 User Manual

Connecting to the Embedded HornetQ

To connect clients to HornetQ you just create the factories as normal:

43.3.1. Core API

If using the core API, just create the ClientSessionFactory and use the regular core API.
ClientSessionFactory nettyFactory =  HornetQClient.createClientSessionFactory(
                                        new TransportConfiguration(
                                           InVMConnectorFactory.class.getName()));

ClientSession session = factory.createSession();

session.createQueue("example", "example", true);

ClientProducer producer = session.createProducer("example");

ClientMessage message = session.createMessage(true);

message.getBody().writeString("Hello");

producer.send(message);

session.start();

ClientConsumer consumer = session.createConsumer("example");

ClientMessage msgReceived = consumer.receive();

System.out.println("message = " + msgReceived.getBody().readString());

session.close();

43.3.2. JMS API

Connection on an Embedded HornetQ through JMS is also simple. Just instantiate ConnectionFactory directly. The following example illustrates that.
ConnectionFactory cf =
    HornetQJMSClient.createConnectionFactory(
       new TransportConfiguration(InVMConnectorFactory.class.getName()));

Connection conn = cf.createConnection();

conn.start();

Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);

MessageProducer prod = sess.createProducer(queue);

TextMessage msg = sess.createTextMessage("Hello!");

prod.send(msg);

sess.commit();

MessageConsumer consumer = sess.createConsumer(queue);

TextMessage txtmsg = (TextMessage)consumer.receive();

System.out.println("Msg = " + txtmsg.getText());

sess.commit();

conn.close();

43.4. JMS Embedding Example

Please see Section 11.2.1, “Embedded” for an example which shows how to setup and run HornetQ embedded with JMS.

No comments: