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

  • 相关阅读:
    使用keepalived实现双机热备
    MYSQL ERROR CODE 错误编号的意义
    Mysql slow query log
    eclipse svn 分支合并到主干
    Timer的schedule和scheduleAtFixedRate方法的区别解析
    Java内部类引用外部类中的局部变量为何必须是final问题解析
    nginx中有关命令和日志切割,配置文件加载的详细阐述
    流媒体中ffmpeg 命令的使用
    windows下搭建nginx服务器及实现nginx支持https配置流程
    mysql 中sql语句的执行顺序
  • 原文地址:https://www.cnblogs.com/pangblog/p/3246934.html
Copyright © 2020-2023  润新知