按照Java EE 7 with GlassFish 4 Application Server中的JMS示例,p 203,我可以发送消息,但似乎无法接收消息:
发送消息:
thufir@doge:~$
thufir@doge:~$ appclient -client NetBeansProjects/MessageSender/dist/MessageSender.jar
Mar 05, 2015 3:47:05 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Mar 05, 2015 3:47:05 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 5.1 (Build 9-b) Compile: July 29 2014 1229
Mar 05, 2015 3:47:05 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is REMOTE, connection mode is TCP
Mar 05, 2015 3:47:05 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:REMOTE
Sending the following message: Testing, 1, 2, 3. Can you hear me?
Sending the following message: Do you copy?
Sending the following message: Good bye!
thufir@doge:~$
等待接收消息:
thufir@doge:~$
thufir@doge:~$ appclient -client NetBeansProjects/MessageReceiver/dist/MessageReceiver.jar
Mar 05, 2015 3:48:11 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Mar 05, 2015 3:48:11 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 5.1 (Build 9-b) Compile: July 29 2014 1229
Mar 05, 2015 3:48:11 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is REMOTE, connection mode is TCP
Mar 05, 2015 3:48:11 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:REMOTE
Waiting for messages...
邮件发件人:
package net.ensode.glassfishbook;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.Queue;
public class MessageSender {
@Resource(mappedName = "jms/CrmConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/CrmQueue")
private static Queue queue;
public void produceMessages() {
JMSContext jmsContext = connectionFactory.createContext();
JMSProducer jmsProducer = jmsContext.createProducer();
String msg1 = "Testing, 1, 2, 3. Can you hear me?";
String msg2 = "Do you copy?";
String msg3 = "Good bye!";
System.out.println("Sending the following message: "
+ msg1);
jmsProducer.send(queue, msg1);
System.out.println("Sending the following message: "
+ msg2);
jmsProducer.send(queue, msg2);
System.out.println("Sending the following message: "
+ msg3);
jmsProducer.send(queue, msg3);
}
public static void main(String[] args) {
new MessageSender().produceMessages();
}
}
消息接收者:
package net.ensode.glassfishbook;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.Topic;
public class MessageReceiver {
@Resource(mappedName = "jms/CrmConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/CrmTopic")
private static Topic topic;
public void getMessages() {
String message;
boolean goodByeReceived = false;
JMSContext jmsContext = connectionFactory.createContext();
JMSConsumer jMSConsumer = jmsContext.createConsumer(topic);
System.out.println("Waiting for messages...");
while (!goodByeReceived) {
message = jMSConsumer.receiveBody(String.class);
if (message != null) {
System.out.print("Received the following message: ");
System.out.println(message);
System.out.println();
if (message.equals("Good bye!")) {
goodByeReceived = true;
}
}
}
}
public static void main(String[] args) {
new MessageReceiver().getMessages();
}
}
glassfish console:
asadmin>
asadmin> list-jms-resources
jms/CrmQueue
jms/CrmTopic
jms/__defaultConnectionFactory
jms/CrmConnectionFactory
jms/CrmQueueConnectionFactory
jms/CrmTopicConnectionFactory
Command list-jms-resources executed successfully.
asadmin>
asadmin> version
Version = GlassFish Server Open Source Edition 4.1 (build 13)
Command version executed successfully.
asadmin>
glassfish日志:
[2015-03-05T02:21:04.258-0800] [glassfish 4.1] [INFO] [] [org.glassfish.jersey.server.ApplicationHandler] [tid: _ThreadID=39 _ThreadName=admin-listener(2)] [timeMillis: 1425550864258] [levelValue: 800] [[
Initiating Jersey application, version Jersey: 2.10.4 2014-08-08 15:09:00...]]
[2015-03-05T02:21:04.321-0800] [glassfish 4.1] [INFO] [NCLS-REST-00001] [javax.enterprise.admin.rest] [tid: _ThreadID=39 _ThreadName=admin-listener(2)] [timeMillis: 1425550864321] [levelValue: 800] [[
Listening to REST requests at context: /command/domain.]]
[2015-03-05T03:42:44.022-0800] [glassfish 4.1] [INFO] [jts.startup_msg] [javax.enterprise.system.core.transaction.com.sun.jts.CosTransactions] [tid: _ThreadID=46 _ThreadName=iiop-service-kernel(1) SelectorRunner] [timeMillis: 1425555764022] [levelValue: 800] [[
JTS5014: Recoverable JTS instance, serverId = [3700]]]
[2015-03-05T03:42:45.389-0800] [glassfish 4.1] [INFO] [] [javax.enterprise.resource.jms] [tid: _ThreadID=158 _ThreadName=p: thread-pool-1; w: 1] [timeMillis: 1425555765389] [levelValue: 800] [[
addresslist.setjmsservice.provider]]
[2015-03-05T03:42:45.390-0800] [glassfish 4.1] [INFO] [] [javax.enterprise.resource.jms] [tid: _ThreadID=158 _ThreadName=p: thread-pool-1; w: 1] [timeMillis: 1425555765390] [levelValue: 800] [[
jms.connection.url]]
[2015-03-05T03:42:45.568-0800] [glassfish 4.1] [INFO] [] [javax.resourceadapter.mqjmsra.lifecycle] [tid: _ThreadID=158 _ThreadName=p: thread-pool-1; w: 1] [timeMillis: 1425555765568] [levelValue: 800] [[
MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 5.1 (Build 9-b) Compile: July 29 2014 1229]]
[2015-03-05T03:42:45.569-0800] [glassfish 4.1] [INFO] [] [javax.resourceadapter.mqjmsra.lifecycle] [tid: _ThreadID=158 _ThreadName=p: thread-pool-1; w: 1] [timeMillis: 1425555765569] [levelValue: 800] [[
MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is EMBEDDED, connection mode is Direct]]
[2015-03-05T03:42:46.816-0800] [glassfish 4.1] [INFO] [] [javax.resourceadapter.mqjmsra.lifecycle] [tid: _ThreadID=158 _ThreadName=p: thread-pool-1; w: 1] [timeMillis: 1425555766816] [levelValue: 800] [[
MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:EMBEDDED]]
thufir@doge:~$
我对本书代码所做的唯一修改就是JMS队列和主题的名称。
答案 0 :(得分:0)
目前我无法解释为什么这个解决方案有效,或者有任何不同之处。但是,它运行正常:
thufir@doge:~$
thufir@doge:~$ appclient -client NetBeansProjects/JMSPTPProducer/dist/JMSPTPProducer.jar
Mar 08, 2015 4:33:07 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Mar 08, 2015 4:33:08 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 5.1 (Build 9-b) Compile: July 29 2014 1229
Mar 08, 2015 4:33:08 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is REMOTE, connection mode is TCP
Mar 08, 2015 4:33:08 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:REMOTE
Sending the following message: Testing, 1, 2, 3. Can you hear me?
Sending the following message: Do you copy?
Sending the following message: Good bye!
thufir@doge:~$
thufir@doge:~$ appclient -client NetBeansProjects/JMSPTPConsumer/dist/JMSPTPConsumer.jar
Mar 08, 2015 4:33:24 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Mar 08, 2015 4:33:24 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 5.1 (Build 9-b) Compile: July 29 2014 1229
Mar 08, 2015 4:33:24 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is REMOTE, connection mode is TCP
Mar 08, 2015 4:33:24 AM com.sun.messaging.jms.ra.ResourceAdapter start
INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:REMOTE
Waiting for messages...
Received the following message: Testing, 1, 2, 3. Can you hear me?
Received the following message: Do you copy?
Received the following message: Good bye!
thufir@doge:~$
生产者:
package net.ensode.glassfishbook;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.Queue;
public class MessageSender {
@Resource(mappedName = "jms/CrmConnectionFactory")
// @Resource(mappedName = "jms/GlassFishBookConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/CrmQueue")
// @Resource(mappedName = "jms/GlassFishBookQueue")
private static Queue queue;
public void produceMessages() {
JMSContext jmsContext = connectionFactory.createContext();
JMSProducer jmsProducer = jmsContext.createProducer();
String msg1 = "Testing, 1, 2, 3. Can you hear me?";
String msg2 = "Do you copy?";
String msg3 = "Good bye!";
System.out.println("Sending the following message: "
+ msg1);
jmsProducer.send(queue, msg1);
System.out.println("Sending the following message: "
+ msg2);
jmsProducer.send(queue, msg2);
System.out.println("Sending the following message: "
+ msg3);
jmsProducer.send(queue, msg3);
}
public static void main(String[] args) {
new MessageSender().produceMessages();
}
}
消费者:
package net.ensode.glassfishbook;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.Queue;
public class MessageReceiver {
@Resource(mappedName = "jms/CrmConnectionFactory")
//@Resource(mappedName = "jms/GlassFishBookConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/CrmQueue")
// @Resource(mappedName = "jms/GlassFishBookQueue")
private static Queue queue;
public void getMessages() {
String message;
boolean goodByeReceived = false;
JMSContext jmsContext = connectionFactory.createContext();
JMSConsumer jMSConsumer = jmsContext.createConsumer(queue);
System.out.println("Waiting for messages...");
while (!goodByeReceived) {
message = jMSConsumer.receiveBody(String.class);
if (message != null) {
System.out.print("Received the following message: ");
System.out.println(message);
System.out.println();
if (message.equals("Good bye!")) {
goodByeReceived = true;
}
}
}
}
public static void main(String[] args) {
new MessageReceiver().getMessages();
}
}
Glassfish中可能存在配置错误,导致无法正确执行。但是,我不这么认为,因为我没有触及此处使用的PTP队列的配置...