Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MessageConsumer.receive()挂起ioexception_Java_Jakarta Ee_Jboss_Jms - Fatal编程技术网

Java MessageConsumer.receive()挂起ioexception

Java MessageConsumer.receive()挂起ioexception,java,jakarta-ee,jboss,jms,Java,Jakarta Ee,Jboss,Jms,使用JBoss5.1,java独立应用程序 我正在尝试侦听从独立应用程序到JBossJMS的消息 问题是,当我关闭jboss服务器时,我在独立应用程序端遇到一个异常,但receive()仍然挂起,因此我的线程被卡住,无法重新连接或执行任何其他操作 这是我的单机版中的例外: 2012-05-31 11:47:18,738 org.jboss.remoting.transport.socket.SocketClientInvoker [ERROR] Got marshalling exception

使用JBoss5.1,java独立应用程序

我正在尝试侦听从独立应用程序到JBossJMS的消息

问题是,当我关闭jboss服务器时,我在独立应用程序端遇到一个异常,但receive()仍然挂起,因此我的线程被卡住,无法重新连接或执行任何其他操作

这是我的单机版中的例外:

2012-05-31 11:47:18,738 org.jboss.remoting.transport.socket.SocketClientInvoker [ERROR] Got marshalling exception, exiting
java.io.IOException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.flush(Unknown Source)
    at java.io.DataOutputStream.flush(Unknown Source)
    at org.jboss.jms.wireformat.SerializedPacket.write(SerializedPacket.java:81)
    at org.jboss.jms.wireformat.JMSWireFormat.write(JMSWireFormat.java:237)
    at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.versionedWrite(MicroSocketClientInvoker.java:971)
    at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.transport(MicroSocketClientInvoker.java:606)
    at org.jboss.remoting.transport.bisocket.BisocketClientInvoker.transport(BisocketClientInvoker.java:418)
    at org.jboss.remoting.MicroRemoteClientInvoker.invoke(MicroRemoteClientInvoker.java:122)
    at org.jboss.remoting.ConnectionValidator.doCheckConnectionWithLease(ConnectionValidator.java:522)
    at org.jboss.remoting.ConnectionValidator.run(ConnectionValidator.java:301)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
我的独立应用程序代码:

    private static Logger log = LoggerFactory.getLogger(MomTopicListenerAbs.class);
    protected String componentCode = null;
    protected int reconnectTimeInterval = 30000;
    MessageConsumer topicMsgConsumer = null;
    Session topicSession = null;
    String momUrl = null;
    Connection topicConnection = null;
    InitialContext jmsContext = null;
    private Thread listenerThread = null;
    boolean shouldListen = true;


@Override
    public void connectToTopic() throws Exception
    {
        TopicConnectionFactory myConnFactory;
        Topic myTopic;
        String MYCF_LOOKUP_NAME = MomConstants.MOM_EXTERNAL_CONNECTION_FACTORY;
        String MYTOPIC_LOOKUP_NAME = MomConstants.DISPATCHER_TOPIC_LOOKUP;
        jmsContext = new InitialContext();
        jmsContext.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        jmsContext.addToEnvironment("java.naming.factory.url.pkgs", "org.jnp.interfaces");
        jmsContext.addToEnvironment(Context.PROVIDER_URL, momUrl);
        myConnFactory = (javax.jms.TopicConnectionFactory) jmsContext.lookup(MYCF_LOOKUP_NAME);
        myTopic = (Topic) jmsContext.lookup(MYTOPIC_LOOKUP_NAME);
        topicConnection = myConnFactory.createConnection();
        topicSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        topicMsgConsumer = topicSession.createConsumer(myTopic);
        topicConnection.start();
    }

@Override
    public void run()
    {
        Message msg = null;
        log.info("Listening to Mom Topic at url=" + momUrl + " started.");
        try
        {

            connectToTopic();
            while (shouldListen)
            {
                try
                {       //this is where it hangs when I kill the jboss  server.
                    msg = topicMsgConsumer.receive();
                    if (msg == null)
                    {
                        throw new Exception("Received empty msg from Topic Listener. could be caused by stopListening() invoke");
                    }
                    executeMsgThread(msg);
                }
                catch (JMSException e)
                {
                    log.error("Caught JMS exception, reconnecting...", e);
                }
                catch (Exception e)
                {
                    log.error("Error in topic Listener. errorMsg=" + e.getMessage(), e);
                }
            }
        }
        catch (Throwable t)
        {
            log.error(TAG + ", System error", t);
        }
我怎样才能抓住那个例外

谢谢, ray。

您需要在客户端的连接上安装一个。当服务器停止时,您将收到来自已注册侦听器的回调

=======更新======


奇怪的是,您没有收到异常侦听器的回调。为了避免失速线程,您可以考虑使用OR。这样,您就不会陷入perma阻塞。

在杀死客户端应用程序之前,您需要等待多长时间?通常,当服务器被终止时,它打开的套接字也将被关闭。客户端应用程序检测套接字关闭并立即引发套接字异常。可能是客户端应用程序中的套接字正在花费时间检测套接字关闭事件,因此异常引发可能已延迟。在JBoss服务器中检查通信设置可能是值得的。在哪里可以检查该设置?