2014-02-22 16:21:02,843 | WARN | Transport Connection to: tcp://10.18.222.21:4751 failed: java.net.SocketException: Connection reset | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: tcp:///10.18.222.21:4751@61616
2014-02-22 16:21:02,843 | WARN | Transport Connection to: tcp://127.0.0.1:4743 failed: java.net.SocketException: Connection reset | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: tcp:///127.0.0.1:4743@61616
在项目中终于解决了这个困扰很久的问题,一直想不到是这个原因,还以为MQ有BUG,
mqConnFactory = new ActiveMQConnectionFactory(config.getMqConfig().getUrl());
connection = mqConnFactory.createConnection();
当时有2个方法都使用mqConnFactory.createConnection();一个是初始化,一个是检查mq状态,mqConnFactory.createConnection();一次就行了!
以下代码是另外一个测试程序,以上面无关。
发送端:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class MessageSendAndReceive {
public static Scheduler sched;
public static void main(String[] args) throws Exception {
MessageSendAndReceive.run();
}
public static void send() throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TestQueue");
Message message = session.createTextMessage("Hello JMS!");
MessageProducer producer = session.createProducer(destination);
producer.send(message);
System.out.println("Send Message Completed!");
// MessageConsumer comsumer = session.createConsumer(destination);
// Message recvMessage = comsumer.receive();
// System.out.println("Received:" + ((TextMessage)
// recvMessage).getText());
producer.close();
session.close();
connection.close();
}
public static void run() throws Exception {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// 通过schedulerFactory获取一个调度器
sched = schedulerFactory.getScheduler();
// 创建jobDetail实例,绑定Job实现类
// 指明job的名称,所在组的名称,以及绑定job类
// 创建LzstoneTimeTask的定时任务
JobDetail jobDetail = JobBuilder.newJob(LzstoneTimeTask.class)
.withIdentity("lzstoneJob", Scheduler.DEFAULT_GROUP).build();
// 目标 创建任务计划
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simpleTrigger", "triggerGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?")).startNow().build();
// 0 0 12 * * ? 代表每天的中午12点触发
// 把作业和触发器注册到任务调度中
sched.scheduleJob(jobDetail, trigger);
// 启动调度
sched.start();
}
// 停止
public static void stop() throws Exception {
sched.shutdown();
}
}
Job:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class LzstoneTimeTask implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
// 执行的定时器任务
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TestQueue");
Message message = session.createTextMessage("Hello JMS!");
MessageProducer producer = session.createProducer(destination);
// 消息为非持久化消息
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 设置心跳消息的存活时间,因为会定时发送,所以,没有必要永久存在。
producer.setTimeToLive(1 * 1000);
producer.send(message);
System.out.println("Send Message Completed!");
// MessageConsumer comsumer = session.createConsumer(destination);
// Message recvMessage = comsumer.receive();
// System.out.println("Received:" + ((TextMessage)
// recvMessage).getText());
producer.close();
session.close();
connection.close();
} catch (Exception e) {
}
}
}
监听:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Jms_Receiver_Listener extends Thread implements MessageListener, ExceptionListener {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session:一个发送或接收消息的线程
Session session;
// Destination :消息的目的地
Destination destination;
// 消息接收者
MessageConsumer consumer;
public Jms_Receiver_Listener() {
String url = "tcp://localhost:61616";
// 在ActiveMq的console配置的queue的名字
String queue = "TestQueue";
connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, url);
// 构造从工厂得到连接对象
try {
connection = connectionFactory.createConnection();
connection.setExceptionListener(this);// 异常处理
connection.start();// 连接启动
// 如果为true,则队列里面的消息没有被取走,继续存在
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 获取session,
destination = session.createQueue(queue);
consumer = session.createConsumer(destination);
} catch (JMSException e) {
System.err.println("Create fail!");
e.printStackTrace();
}
}
public void run() {
try {
consumer.setMessageListener(this);
} catch (JMSException e) {
System.err.println(" MessageListener failed...");
e.printStackTrace();
}
}
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Received: " + msg);
}
} catch (JMSException e) {
System.err.println("The process of getting a message failed...");
e.printStackTrace();
}
}
// 异步消息异常处理
public void onException(JMSException arg0) {
System.err.println("JMS异常!");
}
// 测试程序
public static void main(String[] args) {
Jms_Receiver_Listener jrl = new Jms_Receiver_Listener();
jrl.start();
}
}