activemq java客户端,ActiveMQ客户端配置使用

一、通过JNDI来使用ActiveMQ

1、jndi配置JMS对象

java.naming.factory.initial =org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector

java.naming.provider.url= vm://localhost

# use the following property to specify the JNDI name the connection factory

# should appear as.

#connectionFactoryNames=connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queuesinJNDI using the form

# queue.[jndiName]=[physicalName]

queue.MyQueue=example.MyQueue

# register some topicsinJNDI using the form

# topic.[jndiName]=[physicalName]

topic.MyTopic= example.MyTopic

2、客户端代码使用

//create a new intial context, which loads from jndi.properties file

javax.naming.Context ctx = newjavax.naming.InitialContext();//lookup the connection factory

javax.jms.TopicConnectionFactory factory = (javax.jms.TopicConnectionFactory)ctx.lookup("ConnectionFactory");//create a new TopicConnection for pub/sub messaging

javax.jms.TopicConnection conn =factory.getTopicConnection();//lookup an existing topic

javax.jms.Topic mytopic = (javax.jms.Topic)ctx.lookup("MyTopic");//create a new TopicSession for the client

javax.jms.TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);//create a new subscriber to receive messages

javax.jms.TopicSubscriber subscriber = session.createSubscriber(mytopic);

Notice the name of the topic in the sample is "MyTopic". ActiveMQ will read the jndi.properties files and creates the topics and queues in a lazy fashion. The prefix topic and queue is stripped, so the jndi name begins after the prefix.

Once you have the jndi.properties edited and ready, it needs to be accessible to your application. The easiest way is to add jndi.properties to a jar file. When "new InitialContext()" is called, it will scan the resources and find the file. If you get "javax.naming.NamingException", it usually means the jndi.properties file is not accessible.

3、通过Porperties来设置

Properties props = newProperties();

props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");

props.setProperty(Context.PROVIDER_URL,"tcp://hostname:61616");

javax.naming.Context ctx= new InitialContext(props);

4、Example Java Code

importjavax.jms.Connection;importjavax.jms.ConnectionFactory;importjavax.jms.Destination;importjavax.jms.JMSException;importjavax.jms.MessageProducer;importjavax.jms.Session;importjavax.jms.TextMessage;importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** A simple polymorphic JMS producer which can work with Queues or Topics which

* uses JNDI to lookup the JMS connection factory and destination

*

**/

public final classSimpleProducer {private static final Logger LOG = LoggerFactory.getLogger(SimpleProducer.class);privateSimpleProducer() {

}/***@paramargs the destination name to send to and optionally, the number of

* messages to send*/

public static voidmain(String[] args) {

Context jndiContext= null;

ConnectionFactory connectionFactory= null;

Connection connection= null;

Session session= null;

Destination destination= null;

MessageProducer producer= null;

String destinationName= null;final intnumMsgs;if ((args.length < 1) || (args.length > 2)) {

LOG.info("Usage: java SimpleProducer []");

System.exit(1);

}

destinationName= args[0];

LOG.info("Destination name is " +destinationName);if (args.length == 2) {

numMsgs= (new Integer(args[1])).intValue();

}else{

numMsgs= 1;

}/** Create a JNDI API InitialContext object*/

try{

jndiContext= newInitialContext();

}catch(NamingException e) {

LOG.info("Could not create JNDI API context: " +e.toString());

System.exit(1);

}/** Look up connection factory and destination.*/

try{

connectionFactory= (ConnectionFactory)jndiContext.lookup("ConnectionFactory");

destination=(Destination)jndiContext.lookup(destinationName);

}catch(NamingException e) {

LOG.info("JNDI API lookup failed: " +e);

System.exit(1);

}/** Create connection. Create session from connection; false means

* session is not transacted. Create sender and text message. Send

* messages, varying text slightly. Send end-of-messages message.

* Finally, close connection.*/

try{

connection=connectionFactory.createConnection();

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

producer=session.createProducer(destination);

TextMessage message=session.createTextMessage();for (int i = 0; i < numMsgs; i++) {

message.setText("This is message " + (i + 1));

LOG.info("Sending message: " +message.getText());

producer.send(message);

}/** Send a non-text control message indicating end of messages.*/producer.send(session.createMessage());

}catch(JMSException e) {

LOG.info("Exception occurred: " +e);

}finally{if (connection != null) {try{

connection.close();

}catch(JMSException e) {

}

}

}

}

}

1、使用spring配置ConnectionFactory

tcp://localhost:61616

或者使用Zeroconf来查询可用的brokers

zeroconf://_activemq.broker.development.

如果是使用的是spring2.0以上版本,则不需要显式配置factory bean

xmlns="http://www.springframework.org/schema/beans"xmlns:amq="http://activemq.apache.org/schema/core"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://activemq.apache.org/schema/corehttp://activemq.apache.org/schema/core/activemq-core.xsd">

2、配置使用JmsTemplate,此处使用了ActiveMQ自带的连接池

!-- a pooling based JMS provider -->

tcp://localhost:61616

原文:http://www.cnblogs.com/xuelu/p/3832310.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于ActiveMQ集群的Java实现方案,是吗?那我可以给您一些简单的介绍和建议。 首先,ActiveMQ是一个流行的开源消息中间件,它支持多种协议和消息格式,可以在分布式环境下运行。要实现ActiveMQ集群,有不同的方案和配置可以选择,这里提供一个简单的示例。 在Java中,可以使用ActiveMQJava客户库来连接和发送消息到ActiveMQ Broker,也可以使用Spring Framework提供的ActiveMQ支持来实现更方便的集成和配置。 以下是一个基本的ActiveMQ集群配置示例: 1. 首先,在每个机器上安装ActiveMQ Broker,并确保它们都在同一个网络中,并且可以相互访问。 2. 在每个机器上,创建一个ActiveMQ Broker实例。可以使用命令行或XML配置文件进行配置。例如,使用XML配置文件,可以指定Broker的名称、IP地址、口等参数。 3. 启动每个Broker实例,并确保它们都处于运行状态。可以使用命令行或Java代码启动。 4. 配置Broker之间的网络连接。可以使用静态或动态发现方式进行。静态发现需要手动配置每个Broker的网络连接信息,而动态发现可以使用ActiveMQ提供的多种发现方式,如多播、JMX等。例如,可以在每个Broker的XML配置文件中指定其他Broker的网络地址。 5. 创建一个ActiveMQ连接工厂并配置使用负载均衡模式。可以使用Spring Framework提供的ActiveMQConnectionFactory或自己实现。例如,可以使用RoundRobin方式轮流连接不同的Broker实例。 6. 使用ActiveMQ连接工厂创建一个JMS连接,并从中创建一个JMS会话。可以使用Spring Framework提供的JmsTemplate或自己实现。例如,可以使用JmsTemplate发送和接收JMS消息。 7. 测试集群功能。可以尝试在不同的Broker实例上发送和接收消息,并检查它们是否能够正确地被路由和处理。 以上是一个简单的ActiveMQ集群配置示例,仅供参考。实际上,根据不同的需求和场景,可能需要更复杂的配置和实现方式。建议在实际应用中,根据具体情况选择最适合的方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值