package com.daiy.cons;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import javax.jms.Connection;
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.ObjectMessage;
import javax.jms.TextMessage;
import javax.jms.Topic;
import com.daiy.Student;
import com.daiy.send.ToolSupport;
import com.daiy.test.TestXML;
/**
* 接收类,消费者模型
*/
public class SmsMQRecv extends ToolSupport implements MessageListener, ExceptionListener {
protected int count = 0;
protected int dumpCount = 10;
protected boolean verbose = true;
protected int maximumMessages = 0;
private boolean pauseBeforeShutdown;
private boolean running;
private Session session;
private long sleepTime = 0;
/**构造器
* */
public SmsMQRecv(){
}
public static void main(String[] args) {
SmsMQRecv tool = new SmsMQRecv();
// LoadIni ini =new LoadIni(); //装载配置文件
// ini.setFilename("web" + File.separator + "smsmq.ini");
try{
tool.clientID = null;//ini.readINI("clientID");
tool.durable = false;//ini.readINI("durable").equals("true") ? true : false;
// tool.subject = "SMS.MQ";//ini.readINI("subject");
tool.subject = "TEST.FOO";//ini.readINI("subject");
tool.url = "tcp://localhost:61616";//ini.readINI("url");
tool.topic = false;//ini.readINI("topic").equals("true")? true: false;
tool.sleepTime = 0;//Integer.parseInt(ini.readINI("sleepTime"));
}catch(Exception e){
e.printStackTrace();
}
if (args.length > 4) {
tool.maximumMessages = Integer.parseInt(args[4]);
}
//if (args.length > 6) {
// tool.transacted = "true".equals(args[6]);
//}
tool.run();
}
public void run() {
try {
running = true;
System.out.println("连接到服务器: " + url);
System.out.println("Consuming " + (topic ? "topic" : "queue") + ": " + subject);
System.out.println("Using " + (durable ? "durable" : "non-durable") + " subscription");
Connection connection = createConnection();//建立一个消息池连接
connection.setExceptionListener(this);//设置ExceptionListener为this ,因为this implements ExceptionListener
session = createSession(connection);
MessageConsumer consumer = null;//建立一个消息消费者
if (durable && topic) {
consumer = session.createDurableSubscriber((Topic) destination, consumerName);
} else {
consumer = session.createConsumer(destination);
}
if (maximumMessages <= 0) {
consumer.setMessageListener(this);
}
if (maximumMessages > 0) {
consumeMessagesAndClose(connection, session, consumer);
}
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
/**MessageListener 的abstract 接口
* @param Message 从消息池接收到的消息*/
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
// ObjectMessage Msg = ((ObjectMessage) message);
// String temp = Msg.getStringProperty("pro");
// System.out.println(temp);
// Student sd = (Student)(Msg.getObject());
// System.out.println(sd.getName()+"----"+sd.getAddress());
// if (verbose) System.out.println("Received: " + sd.getAge());
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
String path = writeFile(msg);
System.out.println("Consumer:->收到的消息: " + msg);
TestXML xml = new TestXML();
xml.init();
xml.parserXml(path);
} else {
if (verbose) System.out.println("Received: " + message);
}
if (transacted) {
session.commit();
}
} catch (JMSException e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**ExceptionListener的abstract 接口
*@param JMSException 接收到的JMS异常 */
synchronized public void onException(JMSException ex) {
System.out.println("JMS Exception occured. Shutting down client.");
running = false;
}
/**检测实例是否正在运行*/
synchronized boolean isRunning() {
return running;
}
protected void consumeMessagesAndClose(Connection connection, Session session,
MessageConsumer consumer) throws JMSException, IOException {
System.out.println("系统等待,直到接收到 " + maximumMessages + " 条消息再退出");
while( isRunning()){
Message message = consumer.receive();
onMessage(message);
}
/*
for (int i = 0; i < maximumMessages && isRunning();) {
Message message = consumer.receive(10);
if (message != null) {
i++;
onMessage(message);
}
}*/
System.out.println("Closing connection");
consumer.close();
session.close();
connection.close();
if (pauseBeforeShutdown) {
System.out.println("Press return to shut down");
System.in.read();
}
}
/**
*
* @param res 文件内容
*/
private String writeFile(String res){
BufferedWriter bw = null;
String temp = "";
try {
Calendar time = Calendar.getInstance();
String fileName = time.getTime().getTime()+".xml";
File file = new File("src/xmlFile/"+fileName);
bw = new BufferedWriter(new FileWriter(file));
bw.write(res);
bw.close();
temp = file.getPath();
} catch (Exception e) {
temp = "";
try {
if(bw != null){
bw.close();
}
} catch (IOException e1) {
temp = "";
e1.printStackTrace();
}
e.printStackTrace();
}
return temp;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
activeMQ的一个简单的demo
共84个文件
jar:23个
java:14个
class:14个
4星 · 超过85%的资源 需积分: 10 36 下载量 32 浏览量
2012-08-24
11:45:31
上传
评论
收藏 13.35MB RAR 举报
温馨提示
一个activeMQ的demo,直接导入项目就可以运行,jar以及具体实现方法,配置文件等都是齐全的!
资源推荐
资源详情
资源评论
收起资源包目录
ActiveMQ.rar (84个子文件)
java
activemq-all-5.4.3.jar 3.66MB
testmq
src
com
daiy
ConsumerTool.java 4KB
ProducerTool.java 3KB
Test.java 895B
log4j.properties 2KB
.classpath 518B
.mymetadata 291B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 629B
.myeclipse
.project 1KB
WebRoot
META-INF
MANIFEST.MF 36B
index.jsp 834B
WEB-INF
classes
com
daiy
ConsumerTool.class 3KB
Test.class 1KB
ProducerTool.class 3KB
log4j.properties 2KB
activemq.xml 2KB
lib
activemq-all-5.4.3.jar 3.66MB
LICENSE 40KB
log4j-1.2.11.jar 342KB
NOTICE 3KB
web.xml 620B
mq
.springBeans 462B
src
com
daiy
cons
MQCons.java 4KB
SmsMQRecv.java 7KB
util
XMLAnalyse.java 2KB
Student.java 799B
send
MQSendTool.java 4KB
ToolSupport.java 3KB
MessageConverter.java 68B
SmsMQSender.java 5KB
test
XmlDocument.java 323B
TestXML.java 4KB
Test.java 2KB
applicationContext.xml 347B
listtest.xml 167B
list.xml 198B
smsmq.ini 251B
xmlFile
1336978165312.xml 166B
1336979520890.xml 198B
1336976320953.xml 166B
bin
com
daiy
cons
MQCons.class 4KB
SmsMQRecv.class 6KB
util
XMLAnalyse.class 3KB
send
MessageConverter.class 127B
ToolSupport.class 3KB
MQSendTool.class 4KB
SmsMQSender.class 4KB
test
TestXML.class 5KB
XmlDocument.class 181B
Test.class 2KB
Student.class 1KB
applicationContext.xml 347B
listtest.xml 167B
list.xml 198B
smsmq.ini 251B
xmlFile
1336978165312.xml 166B
1336979520890.xml 198B
1336976320953.xml 166B
.classpath 2KB
.settings
org.eclipse.jdt.core.prefs 629B
.project 586B
lib
spring-beans.jar 477KB
spring-aspects.jar 20KB
activemq-all-5.4.3.jar 3.66MB
aopalliance.jar 4KB
commons-attributes-compiler.jar 29KB
aspectjlib.jar 8KB
spring-core.jar 279KB
spring-agent.jar 5KB
asm-commons-2.2.3.jar 15KB
commons-logging.jar 59KB
aspectjrt.jar 112KB
log4j-1.2.15.jar 383KB
spring-aop.jar 318KB
spring-tomcat-weaver.jar 9KB
commons-attributes-api.jar 35KB
spring-context.jar 466KB
asm-2.2.3.jar 34KB
asm-util-2.2.3.jar 34KB
cglib-nodep-2.1_3.jar 317KB
aspectjweaver.jar 1.47MB
共 84 条
- 1
资源评论
- 岁冄嘸聲2017-11-08非常实用的一个例子
- sixcorner2013-03-25很好,很实用的例子
- maogeer2013-12-25activeMQ的例子,正在学习中。
- lymalf2012-12-10非常实用的一个例子
- 宗轩浚兮2013-12-12还不错,学习用
viper_kenly
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功