原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://dgd2010.blog.51cto.com/1539422/1680244
1
|
apt-get
install
openjdk-7-jdk
|
1
2
3
4
5
|
cd
/tmp/
wget http:
//archive
.apache.org
/dist/activemq/apache-activemq/5
.9.0
/apache-activemq-5
.9.0-bin.
tar
.gz
tar
zxf apache-activemq-5.9.0-bin.
tar
.gz
cd
apache-activemq-5.9.0/
vim bin
/activemq
|
1
|
.
/bin/activemq
console
|
1
|
.
/bin/activemq
start
|
1
2
3
4
|
mkdir
-p com
/jms
mv
App.java com
/jms
javac -
cp
.:activemq-all-5.9.0.jar:
/usr/lib/jvm/java-7-openjdk-amd64/jre
com
/jms/App
.java
java -
cp
.:activemq-all-5.9.0.jar:
/usr/lib/jvm/java-7-openjdk-amd64/jre
com.jms.App
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
//package com.jms;
// Refer:
// Refer:
import
org.apache.activemq.ActiveMQConnection;
import
org.apache.activemq.ActiveMQConnectionFactory;
import
javax.jms.Connection;
import
javax.jms.ConnectionFactory;
import
javax.jms.DeliveryMode;
import
javax.jms.Destination;
import
javax.jms.ExceptionListener;
import
javax.jms.JMSException;
import
javax.jms.Message;
import
javax.jms.MessageConsumer;
import
javax.jms.MessageProducer;
import
javax.jms.Session;
import
javax.jms.TextMessage;
public
class
JmsTestActivemq {
public
static
void
main(String[] args)
throws
JMSException {
ConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://58.67.199.171:61616"
);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(
"jmstestqueue"
);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(
"Hello world!"
);
System.out.println(
"发送消息:Hello world!"
);
producer.send(message);
// session.commit();
session.close();
connection.close();
ConnectionFactory connectionFactory2 =
new
ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://58.67.199.171:61616"
);
Connection connection2 = connectionFactory2.createConnection();
connection2.start();
Session session2 = connection2.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination2 = session2.createQueue(
"jmstestqueue"
);
MessageConsumer consumer = session2.createConsumer(destination2);
while
(
true
) {
TextMessage message2 = (TextMessage) consumer.receive(
1000
);
if
(
null
!= message2)
System.out.println(
"收到消息:"
+ message2.getText());
else
break
;
}
consumer.close();
session2.close();
connection2.close();
}
}
|