异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
在上节中介绍了通过jndi方式连接到hornetq服务器上,有时候由于某些原因,我们不希望通过jndi方式连接,hornetq也支持这种方式进行
以第2章节的例子为模板,我们编写了另一个获取ConnectionFactory的方法createConnection
package com.crazycoder2010.hornetq; import java.util.HashMap; import java.util.Properties; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; import javax.naming.NamingException; import org.hornetq.api.core.TransportConfiguration; import org.hornetq.api.jms.HornetQJMSClient; import org.hornetq.api.jms.JMSFactoryType; import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory; import org.hornetq.core.remoting.impl.netty.TransportConstants; /** * Hello world! * */ public class App4 { public static void main(String[] args) throws Exception { Connection connection = null; try{ connection = createConnection(); //connection = createConnectionWithJNDI(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(HornetQJMSClient.createQueue("exampleQueue")); TextMessage message = session.createTextMessage("Kevin Test01"); System.out.println("Sent message: " + message.getText()); messageProducer.send(message); MessageConsumer consumer = session.createConsumer(HornetQJMSClient.createQueue("exampleQueue")); connection.start(); Message received = consumer.receive(40); System.out.println("received:"+received); }finally{ releaseConnection(connection); } } private static void releaseConnection(Connection connection) throws JMSException { if(connection != null){ connection.close(); } } private static Connection createConnection() throws JMSException { HashMap<String, Object> map = new HashMap<String, Object>(); map.put(TransportConstants.HOST_PROP_NAME, "192.168.1.103"); map.put(TransportConstants.PORT_PROP_NAME, 5445); TransportConfiguration server = new TransportConfiguration(NettyConnectorFactory.class.getName(), map); ConnectionFactory connectionFactory = (ConnectionFactory)HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,server); Connection connection = connectionFactory.createConnection(); return connection; } private static Connection createConnectionWithJNDI() throws NamingException, JMSException{ Properties properties = new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); properties.put("java.naming.provider.url", "jnp://192.168.1.103:1099"); InitialContext initialContext = new InitialContext(properties); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext .lookup("/ConnectionFactory"); Connection connection = connectionFactory.createConnection(); return connection; } }
在如上的代码示例中,我们使用了hornetq客户端提供的一个静态工厂类来创建连接,要设置hornetq的连接地址和端口,这里我们使用默认的5445,这个例子只是在单机本地运行,因此调用HornetQJMSClient的xxxWithoutHA方法来执行,表示我们的连接不许要HA功能