-
1. Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
ataylor Feb 17, 2014 5:52 AM (in response to mssumanth)Im not really sure what you mean by 'The onMessage() does not wait", could you provide some clearer info please, also, code snippets etc or anything that may help us to help you?
-
2. Re: Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
mssumanth Feb 17, 2014 6:43 AM (in response to ataylor)Thanks Andy for the quick reply. Following is the client code I am trying to develop (Jboss 7.1.3. Only jboss-client.jar is in classpath)
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
public class JMSClient implements MessageListener {
public void example() throws Exception
{
String destinationName = "topic/PlayTopic";
Context ic = null;
ConnectionFactory cf = null;
Connection connection = null;
try {
ic = getInitialContext();
cf = (ConnectionFactory)ic.lookup("jms/RemoteConnectionFactory");
Topic topic = (Topic)ic.lookup(destinationName);
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer subscriber = session.createConsumer(topic);
subscriber.setMessageListener(this);
connection.start();
}
finally
{ if(ic != null){
try{
ic.close();
}
catch(Exception e) {
throw e;
}
}
closeConnection(connection);
}
}
public synchronized void onMessage(Message message)
{
TextMessage text = (TextMessage)message;
String strMessage = null;
try {
strMessage = text.getText();
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println("Message received: "+strMessage);
}
private void closeConnection(Connection con) {
try {
if (con != null) {
con.close();
}
}
catch(JMSException jmse) {
System.out.println("Could not close connection " + con +" exception was " + jmse);
}
}
public static void main(String[] args) throws Exception {
new JMSClient().example();
}
public static Context getInitialContext( ) throws javax.naming.NamingException {
Properties p = new Properties( );
p.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
p.put("java.naming.provider.url", "remote://localhost:4447");
return new javax.naming.InitialContext(p);
}
}
Since the MessageListener is set to this very object, the onMessage() waits asynchronously for topic messages in Jboss 4. However, that is not the case in jboss 7.
-
3. Re: Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
ataylor Feb 17, 2014 6:51 AM (in response to mssumanth)Since the MessageListener is set to this very object, the onMessage() waits asynchronously for topic messages in Jboss 4. However, that is not the case in jboss 7.
you need to clarify what you mean by 'waits asynchronously', that doesnt really make much sense, onMessage is called asynchronously by HornetQ there is no waiting any where?
-
4. Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
mssumanth Feb 17, 2014 7:53 AM (in response to ataylor)You are right. I am not able to make onMessage() wiat for oncoming messages.
Jboss 7 appserver is started and the topic gets created and while the appserver is up and running JMSClient is started to listen to any message published onto the said topic. However, what happens is that JMSClient starts and completes the execution immediately ( say, within 1 or 2 seconds) with a log printed on the console:
INFO: JBoss Remoting version 3.2.8.SP1
Feb 17, 2014 3:19:02 PM org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1$MessageReceiver handleEnd
ERROR: Channel end notification received, closing channel Channel ID 8d1db2db (outbound) of Remoting connection 00ba5c7a to null
Due of this, I am not able to listen to any messages published thereafter onto the topic. Is this expected? Is there any configuration that needs to be done in order to get the above program to continue listen for oncoming messages on the topic.
-
5. Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
ataylor Feb 17, 2014 8:09 AM (in response to mssumanth)to be honest, i still dont understand your issue, why would the onMessage method wait, its asynchronous.
i would start by looking at the many examples that ship, maybe one of those can help you?
-
6. Re: Re: Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
mnovak Feb 17, 2014 12:19 PM (in response to mssumanth)1 of 1 people found this helpfulI can see the problem. Method example() exits before any message is received. This is because you call connection.start(); and then exits the program. You would have to add Thread.sleep(...) or some check that last message was received.
You can also use classic synchronous receive like:
while ((message = subscriber.receive(...)) != null) { // do something with message }
in example() method.
I don't know how this code behaved in JBoss 4 but as it behaves now is expected behavior.
-
7. Re: onMessage() does not wait unlike in Jboss 4. Is any configuration missed by me?
mssumanth Feb 18, 2014 4:27 AM (in response to mnovak)@Miroslav
Thanks. Will try this approach.