在 Glassfish 上使用 JMS 向世界打招呼

Posted

技术标签:

【中文标题】在 Glassfish 上使用 JMS 向世界打招呼【英文标题】:hello world with JMS on Glassfish 【发布时间】:2015-05-06 19:13:12 【问题描述】:

按照Java EE 7 with GlassFish 4 Application Server, p 203 中的 JMS 示例,我可以发送消息,但似乎无法接收消息:

发送消息:

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 控制台:

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>  

玻璃鱼原木:

[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 队列和主题的名称。

【问题讨论】:

我正在尝试从 asadmin CLI 获取 jms details from glassfish。 【参考方案1】:

目前我无法解释为什么这个解决方案有效,或者有什么不同。但是,它运行正确:

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队列的配置……

【讨论】:

以上是关于在 Glassfish 上使用 JMS 向世界打招呼的主要内容,如果未能解决你的问题,请参考以下文章

我可以使用哪些 maven 依赖项为 Glassfish 创建独立的 JMS 客户端?

无法通过 Glassfish 4.1 Web 管理员创建任何 JMS 资源

GlassFish中可以使用哪些工具来监控jms broker

尝试从 Glassfish 获取 JMS 资源时出现 javax.naming.NoInitialContextException

Glassfish JMS(平面文件)身份验证

将远程 JMS 客户端连接到 GlassFish 3