博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
阅读量:7062 次
发布时间:2019-06-28

本文共 3115 字,大约阅读时间需要 10 分钟。

在上节中介绍了通过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
map = new HashMap
(); 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功能

 

 

转载地址:http://uwill.baihongyu.com/

你可能感兴趣的文章
俄罗斯食品来中国“过年”
查看>>
市场监管总局:保健食品应标注不具疾病预防治疗功能
查看>>
GitHub 上开源的区块链项目 90% 死亡了
查看>>
澳网张帅首夺大满贯 女双携斯托瑟挑落卫冕冠军
查看>>
“平潭-高雄”货运直航开通 三大优势凸显
查看>>
“共度欢乐春节”摄影图片展在阿斯塔纳开幕
查看>>
新光大ArtPark9亮相 以“艺术”再造生活方式
查看>>
关于Python数据分析,这里有一条高效的学习路径
查看>>
三亚:严查“先登记支付房款、后补交社保或个税”行为
查看>>
神级程序猿用HTML5代码画出恐龙求欢图,想象力太丰富!
查看>>
谋势、聚力、强生态,用友三十而立
查看>>
python爬虫——40行代码爬取「笔趣看」全部小说
查看>>
数据分析师完整的知识结构
查看>>
Airbnb个性化搜索服务架构
查看>>
当大多数人对Vue理解到炉火纯青的时候,是不是该思考一下怎么让vue页面骚气起来...
查看>>
Vue.js从Virtual DOM映射到真实DOM的过程
查看>>
【译】高阶函数:利用Filter、Map和Reduce来编写更易维护的代码
查看>>
三种方法,刷新 Android 的 MediaStore!让你保存的图片立即出现在相册里!
查看>>
Web存储之localStore 与 sessionStore
查看>>
Java迭代器spliterator
查看>>