java - Message Not Redelivered Using Jboss Topic -
i using jboss 4.0.2 ga
i using testtopic defined in jbossmq-destinations-service
<mbean code="org.jboss.mq.server.jmx.topic" name="jboss.mq.destination:service=topic,name=testtopic"> <depends optional-attribute-name="destinationmanager">jboss.mq:service=destinationmanager</depends> <depends optional-attribute-name="securitymanager">jboss.mq:service=securitymanager</depends> <attribute name="securityconf"> <security> <role name="guest" read="true" write="true"/> <role name="publisher" read="true" write="true" create="false"/> <role name="durpublisher" read="true" write="true" create="true"/> </security> </attribute> <attribute name="redeliverydelay">0</attribute> </mbean>
i have written same file publisher , subscriber ::
package com.nagarro.client; import javax.jms.*; import javax.naming.*; import java.io.*; import java.util.properties; public class chat implements javax.jms.messagelistener { private topicsession pubsession; private topicsession subsession; private topicpublisher publisher; private topicconnection connection; private string username; /* constructor. establish jms publisher , subscriber */ public chat(string topicname, string username, string password) throws exception { // obtain jndi connection properties properties = new properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); properties.setproperty(context.provider_url, "localhost:1099"); // ... specify jndi properties specific vendor initialcontext jndi = new initialcontext(properties); // jms connection factory topicconnectionfactory confactory = (topicconnectionfactory) jndi .lookup("topicconnectionfactory"); // create jms connection topicconnection connection = confactory.createtopicconnection(); // create 2 jms session objects topicsession pubsession = connection.createtopicsession(false, session.client_acknowledge); topicsession subsession = connection.createtopicsession(false, session.client_acknowledge); // jms topic topic chattopic = (topic) jndi.lookup(topicname); // create jms publisher , subscriber topicpublisher publisher = pubsession.createpublisher(chattopic); topicsubscriber subscriber = subsession.createsubscriber(chattopic); // set jms message listener subscriber.setmessagelistener(this); // intialize chat application set(connection, pubsession, subsession, publisher, username); // start jms connection; allows messages delivered connection.start(); } /* initialize instance variables */ public void set(topicconnection con, topicsession pubsess, topicsession subsess, topicpublisher pub, string username) { this.connection = con; this.pubsession = pubsess; this.subsession = subsess; this.publisher = pub; this.username = username; } /* receive message topic subscriber */ public void onmessage(message message) { try { textmessage textmessage = (textmessage) message; string text = textmessage.gettext(); system.out.println(text); if(textmessage!=null){ throw new nullpointerexception(); } } catch (jmsexception jmse) { jmse.printstacktrace(); } } /* create , send message using topic publisher */ protected void writemessage(string text) throws jmsexception { textmessage message = pubsession.createtextmessage(); message.settext(username + " : " + text); publisher.publish(message); } /* close jms connection */ public void close() throws jmsexception { connection.close(); } /* run chat client */ public static void main(string[] args) { try { if (args.length != 3) system.out.println("topic or username missing"); // args[0]=topicname; args[1]=username; args[2]=password chat chat = new chat("topic/testtopic", "", ""); // read command line bufferedreader commandline = new java.io.bufferedreader( new inputstreamreader(system.in)); // loop until word "exit" typed while (true) { string s = commandline.readline(); if (s.equalsignorecase("exit")) { chat.close(); // close down connection system.exit(0);// exit program } else chat.writemessage(s); } } catch (exception e) { e.printstacktrace(); } } }
in code above using client_acknowledge
session mode, not getting redelivery of message if throwing nullpointerexception
onmessage
method of listener.
kindly let me know if there configuration change required redelivery happen.
in case of topic, messages redelivered @ once, if call topicsession.recover()
, after have read messages (for example, in try/catch block, if exception happened).
a test using jboss 7, non-durable topic, session.client_acknowledge
, , setmessagelistener
:
topicsubscriber.setmessagelistener(new messagelistener() { @override public void onmessage(message message) { try { throw new illegalstateexception("test"); } catch (exception ex) { try { topicsession.recover(); } catch (jmsexception e) { e.printstacktrace(); } ex.printstacktrace(); } } });
results in immediate redelivery, 10 times, after message put "dead letter queue".
Comments
Post a Comment