HornetQ 2.0.0.GA Example: embedded, no jndi, configured programmatically

The following code is based on the example provided in the hornetq-2.0.0.GA-src zip file, specifically the instantiate-connection-factory example located in the examples/jms/instantiate-connection-factory/ directory.

The code below is written in Groovy and requires jboss-logging to be loaded manually, as demonstrated here:

grape install org.jboss.logging jboss-logging 2.1.0.GA

Once you’ve loaded the jboss-logging dependency, the code should run as-is using the GroovyConsole.


@GrabResolver(name='JBoss releases', root='https://repository.jboss.org/nexus/content/repositories/releases/')
@Grab(group='org.hornetq', module='hornetq-core', version='2.0.0.GA')
@Grab(group='org.hornetq', module='hornetq-jms', version='2.0.0.GA')
@Grab(group='org.hornetq', module='hornetq-logging', version='2.0.0.GA')
@Grab(group='org.hornetq', module='hornetq-transports', version='2.0.0.GA')
@Grab(group='org.jboss.netty', module='netty', version='3.1.5.GA')
@Grab(group='org.jboss.javaee', module='jboss-jms-api', version='1.1.0.GA')
@Grab(group='org.jboss.logging', module='jboss-logging', version='2.1.0.GA')
@GrabResolver(name='Maven Central', root='http://repo1.maven.org/')
@Grab(group='jboss', module='jnpserver', version='4.2.2.GA')
@GrabConfig(systemClassLoader=true)

import java.util.HashMap
import java.util.Map
import javax.jms.Connection
import javax.jms.ConnectionFactory
import javax.jms.MessageConsumer
import javax.jms.MessageProducer
import javax.jms.Queue

import javax.jms.Session
import javax.jms.TextMessage
import org.hornetq.api.core.TransportConfiguration
import org.hornetq.api.jms.HornetQJMSClient
import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory
import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory

import org.hornetq.core.config.Configuration
import org.hornetq.core.config.impl.ConfigurationImpl
import org.hornetq.core.server.cluster.QueueConfiguration
import org.hornetq.core.server.HornetQServer
import org.hornetq.core.server.impl.HornetQServerImpl

import org.hornetq.jms.server.JMSServerManager
import org.hornetq.jms.server.impl.JMSServerManagerImpl
import org.hornetq.jms.server.config.JMSConfiguration
import org.hornetq.jms.server.config.impl.JMSConfigurationImpl
import org.hornetq.jms.server.config.QueueConfiguration
import org.hornetq.jms.server.config.impl.QueueConfigurationImpl
 
def QUEUE_NAME = "exampleQueue", JMS_QUEUE_NAME = "/queue/${QUEUE_NAME}"
 
HornetQServer server = null
Connection connection = null

try {

Configuration configuration = new ConfigurationImpl ()

configuration.setPersistenceEnabled(false)
configuration.setSecurityEnabled(false)
configuration.setClustered(false)
configuration.setClusterUser("blah")
configuration.setClusterPassword("not-used")
configuration.setJMXManagementEnabled(false)
configuration
    .getAcceptorConfigurations()
        .add(
            new TransportConfiguration(
                InVMAcceptorFactory.class.getName())
    )

server = new HornetQServerImpl (configuration)

JMSConfiguration jmsConfiguration = new JMSConfigurationImpl ()

def queueConfigurations = jmsConfiguration.getQueueConfigurations()

queueConfigurations << new QueueConfigurationImpl (QUEUE_NAME, null, false, JMS_QUEUE_NAME)

JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, jmsConfiguration)

// We're not using JNDI.
jmsServerManager.setContext(null)
jmsServerManager.start()

// -----

Queue queue = HornetQJMSClient.createQueue("${QUEUE_NAME}")

TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName())

ConnectionFactory cf = HornetQJMSClient.createConnectionFactory(transportConfiguration)

connection = cf.createConnection()

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)

MessageProducer producer = session.createProducer(queue)

TextMessage message = session.createTextMessage("This is a text message")

System.out.println("Sent message: " + message.getText())

producer.send(message)

MessageConsumer messageConsumer = session.createConsumer(queue)

connection.start()

TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000)

System.out.println("Received message: " + messageReceived.getText())

connection.close()

} finally {
    if (connection != null) {
        connection.close ()
    }
    if (server != null) {
        server.stop ()
    }
}
Summary
Article Name
HornetQ 2.0.0.GA Example: embedded, no jndi, configured programmatically
Description
The following example is based on the example provided in the <a title="hornetq-2.0.0.GA-src zip file" href="http://www.jboss.org/hornetq/downloads" target="_blank" rel="noopener">hornetq-2.0.0.GA-src zip file</a>, specifically the instantiate-connection-factory example located in the examples/jms/instantiate-connection-factory/ directory. The code in this example is written in Groovy and requires the jboss-logging dependency to be loaded manually.
Author
Publisher Name
Coherent Logic Limited
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *