问题
Following the JMS example in Java EE 7 with GlassFish 4 Application Server, p 203, I can send messages, but but can't seem to receive messages:
sending a message:
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:~$
waiting to receive the message:
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...
message sender:
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();
}
}
message receiver:
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 logs:
[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:~$
The only modification I made to the code from the book was to the name for the JMS queue and topic.
回答1:
At present I cannot explain why this solution works, or is in any way different. However, it runs correctly:
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:~$
producer:
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();
}
}
consumer:
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();
}
}
There might be a misconfiguration in Glassfish, which was preventing the correct execution. However, I don't think so, because I didn't touch the configuration of the PTP queue used here...
来源:https://stackoverflow.com/questions/28877487/hello-world-with-jms-on-glassfish