使用 jboss 的 JMS

Posted

技术标签:

【中文标题】使用 jboss 的 JMS【英文标题】:JMS using jboss 【发布时间】:2013-09-20 16:14:19 【问题描述】:

我是 JMS 的新手。当我运行独立的 JMS 测试程序时,出现异常

我得到的异常如下:

**Channel end notification received, closing channel Channel ID 4995329b (inbound) of Remoting connection 0aff5851 to null**

我在下面附上我的代码供您参考:

QueueReceiver.java

package com.tradier.webservices.util;

    import java.util.Hashtable;

    import javax.jms.*;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    public class QueueReceive 
    private final static String JNDI_FACTORY =
    "org.jboss.naming.remote.client.InitialContextFactory";//org.jboss.naming.remote.client.InitialContextFactory
    private final static String JMS_FACTORY = "java:jboss/exported/jms/RemoteConnectionFactory";
    private final static String QUEUE = "queue/test";
    private final static String jbossUrl = "remote://localhost:4447";

    private static InitialContext getInitialContext() throws NamingException 
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, jbossUrl);
    env.put(Context.SECURITY_PRINCIPAL, "jms");
    env.put(Context.SECURITY_CREDENTIALS, "jboss1");
    return new InitialContext(env);
    

    public static void main(String[] args) throws Exception 
    InitialContext ic = getInitialContext();
    QueueConnectionFactory qconFactory = (QueueConnectionFactory)ic.lookup(JMS_FACTORY);
    QueueConnection qcon = qconFactory.createQueueConnection("jms","jboss1");
    QueueSession qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = (Queue)ic.lookup(QUEUE);
    QueueReceiver qreceiver = qsession.createReceiver(queue);

    qcon.start();

    TextMessage msg = (TextMessage)qreceiver.receive();
    System.out.println("MYTEXT"+msg.getText());

    qreceiver.close();
    qsession.close();
    qcon.close();
    
    

QueueSender.java

 package com.tradier.webservices.util;

    import java.util.Hashtable;
    import javax.jms.Queue;
    import javax.jms.QueueConnection;
    import javax.jms.QueueConnectionFactory;
    import javax.jms.QueueSender;
    import javax.jms.QueueSession;
    import javax.jms.Session;
    import javax.jms.TextMessage;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    public class QueueSend 
    private final static String JNDI_FACTORY =
    "org.jboss.naming.remote.client.InitialContextFactory";
    private final static String JMS_FACTORY = "jms/RemoteConnectionFactory";
    private final static String QUEUE = "jms/queue/test";
    private final static String jbossUrl = "remote://localhost:4447";

    private static InitialContext getInitialContext() throws NamingException 
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, jbossUrl);
    env.put(Context.SECURITY_PRINCIPAL, "qtest");
    env.put(Context.SECURITY_CREDENTIALS, "mytest");
    return new InitialContext(env);
    

    public static void main(String[] args) throws Exception 
    InitialContext ic = getInitialContext();
    try
    QueueConnectionFactory qconFactory =
    (QueueConnectionFactory)ic.lookup(JMS_FACTORY);

    QueueConnection qcon =
    qconFactory.createQueueConnection("qtest","mytest");
    QueueSession qsession = qcon.createQueueSession(false,
    Session.AUTO_ACKNOWLEDGE);
    Queue queue = (Queue)ic.lookup(QUEUE);
    QueueSender qsender = qsession.createSender(queue);

    qcon.start();

    TextMessage msg = qsession.createTextMessage();;
    msg.setText("HelloWorld");
    qsender.send(msg);

    qsender.close();
    qsession.close();
    qcon.close();
    
    catch(Exception e)
        System.out.println(e);
    
    
    

jboss Standalone.xml 文件

<subsystem xmlns="urn:jboss:domain:messaging:1.1">
                <hornetq-server>
                    <persistence-enabled>true</persistence-enabled>
                    <security-enabled>false</security-enabled>
                    <journal-file-size>102400</journal-file-size>
                    <journal-min-files>2</journal-min-files>

                    <connectors>
                        <netty-connector name="netty" socket-binding="messaging"/>
                        <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                            <param key="batch-delay" value="50"/>
                        </netty-connector>
                        <in-vm-connector name="in-vm" server-id="0"/>
                    </connectors>

                    <acceptors>
                        <netty-acceptor name="netty" socket-binding="messaging"/>
                        <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                            <param key="batch-delay" value="50"/>
                            <param key="direct-deliver" value="false"/>
                        </netty-acceptor>
                        <in-vm-acceptor name="in-vm" server-id="0"/>
                    </acceptors>

                    <broadcast-groups>
                        <broadcast-group name="bg-group1">
                            <group-address>231.7.7.7</group-address>
                            <group-port>9876</group-port>
                            <broadcast-period>5000</broadcast-period>
                            <connector-ref>
                                netty
                            </connector-ref>
                        </broadcast-group>
                    </broadcast-groups>

                    <discovery-groups>
                        <discovery-group name="dg-group1">
                            <group-address>231.7.7.7</group-address>
                            <group-port>9876</group-port>
                            <refresh-timeout>10000</refresh-timeout>
                        </discovery-group>
                    </discovery-groups>

                    <cluster-connections>
                        <cluster-connection name="my-cluster">
                            <address>jms</address>
                            <connector-ref>netty</connector-ref>
                            <discovery-group-ref discovery-group-name="dg-group1"/>
                        </cluster-connection>
                    </cluster-connections>

                    <security-settings>
                        <security-setting match="#">
                            <permission type="send" roles="guest"/>
                            <permission type="consume" roles="guest"/>
                            <permission type="createNonDurableQueue" roles="guest"/>
                            <permission type="deleteNonDurableQueue" roles="guest"/>
                        </security-setting>
                    </security-settings>

                    <address-settings>
                        <address-setting match="#">
                            <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                            <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                            <redelivery-delay>0</redelivery-delay>
                            <max-size-bytes>10485760</max-size-bytes>
                            <address-full-policy>BLOCK</address-full-policy>
                            <message-counter-history-day-limit>10</message-counter-history-day-limit>
                            <redistribution-delay>1000</redistribution-delay>
                        </address-setting>
                    </address-settings>

                    <jms-connection-factories>
                        <connection-factory name="InVmConnectionFactory">
                            <connectors>
                                <connector-ref connector-name="in-vm"/>
                            </connectors>
                            <entries>
                                <entry name="java:/ConnectionFactory"/>
                            </entries>
                        </connection-factory>
                        <connection-factory name="RemoteConnectionFactory">
                            <connectors>
                                <connector-ref connector-name="netty"/>
                            </connectors>
                            <entries>
                                <entry name="RemoteConnectionFactory"/>
                                <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                            </entries>
                        </connection-factory>
                        <pooled-connection-factory name="hornetq-ra">
                            <transaction mode="xa"/>
                            <connectors>
                                <connector-ref connector-name="in-vm"/>
                            </connectors>
                            <entries>
                                <entry name="java:/JmsXA"/>
                            </entries>
                        </pooled-connection-factory>
                    </jms-connection-factories>

                    <jms-destinations>
                        <jms-queue name="testQueue">
                            <entry name="queue/test"/>
                            <entry name="java:jboss/exported/jms/queue/test"/>
                        </jms-queue>
                    </jms-destinations>
                </hornetq-server>
            </subsystem>

帮助我解决这个问题。 提前致谢。

【问题讨论】:

【参考方案1】:

你能澄清你在哪里得到这个错误吗? 在发件人程序或收件人程序中? 这通常发生在您的 Sender 本身失败并终止,但您的 Receiver 仍在等待接收连接时。在这种情况下,上述错误出现在 Receiver 的控制台输出中,而主要异常显示在 Sender 的控制台中。

这种情况下的主要异常可能是由于用户不存在(例如在 ApplicationRealm 中)或用户可能没有正确的角色而导致的安全异常。

【讨论】:

以上是关于使用 jboss 的 JMS的主要内容,如果未能解决你的问题,请参考以下文章

JNDI 在 JMS 中使用 JBoss 查找错误

为第三方 JMS 提供者使用 JBoss 的通用资源适配器的缺点

如何使用 jboss-cli 在 WildFly 17 中创建 JMS 队列

jboss 6远程jms网桥配置不起作用

如何在 JBoss 5 中订购 EJB 和 JMS 队列配置的部署?

jboss.jms 和 jboss.mq 有啥区别?