jboss - How to handle a Connection object to remote jms server -


we using application contains jboss @service mbean encapsulates javax.jms.connection object.

during startup of mbean connection created initializing remote initialcontext, looking connectionfactory context, , creating connection factory:

@service public class jmspublisher extends etcc.... {    private connection connection;    protected void startservice() {       context ctx = getremoteinitialcontext();       connectionfactory connectionfactory = (connectionfactory) ctx.lookup("connectionfactory");       connection = connectionfactory.createconnection();    } } 

my question is: how long can supposed maintain connection ? in practise see connection throws jmsexception when try create session on after undefined amount of time.

the documentation of connection tells object represents socket, timeouts due inactivity normal. how can deal without creating new connections each , every message ?

your best bet have jmspublisher implement javax.jms.exception listener. implement connect() method safely acquires connection on:

  1. startservice
  2. onexception

a couple of points:

  • for code compression, acquire jms connection factory via resource injection. connection factory reference resolved before startservice called , act implicit depends, making jms connection factory dependency service.

  • have jmspublisher extend org.jboss.system.servicembeansupport , implement token mbean interface (jmspublishermbean) extends org.jboss.system.servicembean. ensure dependencies honoured on service start (and stop).

resource injected jms connection factory

@resource(mappedname="connectionfactory")  private javax.jms.connectionfactory connectionfactory; private volatile javax.jms.connection connection; 

modified startservice()

public void startservice() {    connect(); } 

connection exception handler

public void onexception(jmsexception je) {    connect(); } 

*safe connection initializer (adding conn.start()) *

private void synchronized connect() {     log.info("initializing connection....");     try {         if(connection!=null) {            try { connection.stop(); } catch (exception e) {}            try { connection.close(); } catch (exception e) {}         }         connection = connectionfactory.createconnection();         connection.setexceptionlistener(this);         connection.start();     } catch (exception e) {         log.error("failed intialize jms connection", e);     } } 

this not automatically take care of other jms resources allocated through lost connection, if other components using connection held component, can publish jmx notifications jmspublisher indicating connection has been lost , clean up/re-acquire on notification receipt.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -