0

使用 jboss 5.1,java 独立应用程序。

我正在尝试收听从独立应用程序到 jboss jms 的消息。

问题是当我杀死 jboss 服务器时,我在独立应用程序端出现异常,但接收()仍然挂起,因为我的线程被卡住了,我无法重新连接或做任何其他事情。

那是我独立的例外:

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);
        }

我将如何捕捉该异常?

谢谢,雷。

4

1 回答 1

0

您需要在客户端的连接上添加一个ExceptionListener。当服务器停止时,你会从你注册的监听器那里得到一个回调。

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

奇怪的是,您没有从异常侦听器中获得回调。为避免线程停滞,您可以考虑使用超时版本的接收设置消息侦听器。这样你就不会处于永久块中。

于 2012-05-31T09:55:55.563 回答