JMS - Using Topics on WebSphere Application Server 7 -
i have sender , receiver particular topic. i'm running sender , receiver servlets on 7.0. topic
, topic connection factory
being setup on was. i'm not able receive message send. works fine when try queue instead of topic.
below code use.
public class commonservlet extends httpservlet{ private static final long serialversionuid = 1l; private static boolean initialized = false; private static initialcontext initialcontext = null; private connectionfactory connectionfactory = null; private destination destination = null; protected final void initialize(){ try{ if( initialized == false ){ // jndi initial context initialcontext = new initialcontext(); // flag initialized initialized = true; } } catch( throwable t ){ system.out.println( t.getmessage() ); } } /** * @return */ protected final destination getdestination(){ if( destination != null ){ return destination; } try{ destination = (destination) initialcontext.lookup("jms/testtopic" ); } catch( namingexception e ){ // todo auto-generated catch block e.printstacktrace(); } return destination; } /** * @return */ protected final connectionfactory getconnectionfactory(){ try{ connectionfactory = (connectionfactory) initialcontext.lookup("jms/testtopiccf" ); } catch( namingexception e ){ e.printstacktrace(); } return connectionfactory; } }
sender servlet class
public class sender extends commonservlet{ private static final long serialversionuid = 1l; @override protected void doget( httpservletrequest req, httpservletresponse resp ) throws servletexception, ioexception{ system.out.println("inside of sender"); dopost( req, resp ); } @override protected void dopost( httpservletrequest req, httpservletresponse resp ) throws servletexception, ioexception{ string message = req.getparameter( "message" ); sendmessage( message ); } private void sendmessage( string messagetext ){ initialize(); try{ connectionfactory factory = getconnectionfactory(); destination destination = getdestination(); connection connection = factory.createconnection(); connection.start(); session session = connection.createsession( false, session.auto_acknowledge ); messageproducer sender = session.createproducer( destination ); textmessage txtmsg = session.createtextmessage( messagetext ); sender.send( txtmsg ); } catch( exception ex ){ ex.printstacktrace(); } } }
receiver servlet class
public class receiver extends commonservlet{ private static final long serialversionuid = 1l; @override protected void doget( httpservletrequest req, httpservletresponse resp ) throws servletexception, ioexception{ dopost( req, resp ); } @override protected void dopost( httpservletrequest req, httpservletresponse resp ) throws servletexception, ioexception{ getmessage(); } private void getmessage(){ initialize(); connectionfactory factory = getconnectionfactory(); destination destination = getdestination(); try{ connection connection = factory.createconnection(); connection.start(); session session = connection.createsession( false, session.auto_acknowledge ); messageconsumer receiver = session.createconsumer( destination ); message message = receiver.receive( 4000 ); system.out.println( message );//coming null } catch( jmsexception e ){ e.printstacktrace(); } } }
both testtopic
, testtopiccf
configured in administration console under resources > jms > topic connection factory , topics section. there no exceptions while running application. works fine if use queue , queue connection factory created. there need topic working?
topics different destinations queues, default not persist messages , subscriber has connected, when publisher sends message. see details here
publishers , subscribers have timing dependency. client subscribes topic can consume messages published after client has created subscription, , subscriber must continue active in order consume messages.
so in short:
- your current design not correct topics, have first call receiver servlet, have there long receive timeout, , in second window try sender servlet, message lost.
- much better approach use mdb message receiver instead of servlet
- if need receive messages sends topic while subscriber inactive, need configure durable subscriber , configure topic durable in was.
Comments
Post a Comment