异步消息总线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功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值