I implemented a JMS Client that is supposed to update the browser when a JMS Message arrives. Unfortunately, about 50 per cent of the messages are lost and never arrive. It is really strange. What could be the reason?
Here I post part of my code. I created the client according to the html5 websocket examples for weblogic and I have version 12.1.2. I have no errors on java nor on javascript side. The console prints Receive message from Bean: Shit happens ist printed every two seconds, but I receive only on the client every 4 seconds or even less. Why is that?
package com.packtpub.techbuzz.jms;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.jms.JMSException;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.websocket.ClosingMessage;
import weblogic.websocket.WebSocketAdapter;
import weblogic.websocket.WebSocketConnection;
import weblogic.websocket.WebSocketListener;
import weblogic.websocket.annotation.WebSocket;
@WebSocket(timeout = -1, pathPatterns = "/counter.ws")
public class SocketMediator extends WebSocketAdapter implements WebSocketListener, MessageListener {
private static SocketMediator sm;
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "jms/myConnectionFactory";
private static final String RETURN_QUEUE = "jms/TestJMSReturnQueue";
private static final String QUEUE = "jms/myTestQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private QueueSender qsender;
private javax.jms.Queue queueReturn;
private javax.jms.Queue queue;
private boolean isConnected;
public static SocketMediator getSm() {
return sm;
}
public SocketMediator() {
super();
sm = this;
isConnected = false;
try {
InitialContext ctx = new InitialContext();
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (javax.jms.Queue) ctx.lookup(QUEUE);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qsender = qsession.createSender(queue);
qcon.start();
isConnected = true;
int counter = 0;
// Use the ThreadScheduledExecutor to invoke a runnable at a later time
// which posts some data into the queue
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
try {
try {
TextMessage textMessage = qsession.createTextMessage("Shit happens");
qsender.send(textMessage);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception ex) {
}
}
}, 2, 1, TimeUnit.SECONDS);
} catch (JMSException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}
@Override
public void onOpen(WebSocketConnection webSocketConnection) {
System.out.println("New connection was created from a client " + webSocketConnection.getRemoteAddress());
super.onOpen(webSocketConnection);
}
@Override
public void onMessage(WebSocketConnection connection, String payload) {
// Sends message from the browser back to the client.
System.out.println("Does this ever actually fire?");
}
@Override
public void onClose(WebSocketConnection webSocketConnection, ClosingMessage closingMessage) {
super.onClose(webSocketConnection, closingMessage);
System.out.println("Connection was closed from a client " + webSocketConnection.getRemoteAddress());
}
@Override
public void onError(WebSocketConnection webSocketConnection, Throwable throwable) {
super.onError(webSocketConnection, throwable);
System.out.println("Something went seriously wrong with client " + webSocketConnection.getRemoteAddress());
}
// Subscription for return messages
@Override
public void onMessage(javax.jms.Message msg) {
final String msgText;
try {
if (msg instanceof TextMessage) {
msgText = ((TextMessage) msg).getText();
} else {
msgText = msg.toString();
}
System.out.println("Received message from bean: "+msgText);
for(final WebSocketConnection conn : getWebSocketContext().getWebSocketConnections()){
Executors.newSingleThreadScheduledExecutor().schedule(
new Runnable() {
@Override
public void run() {
try {
conn.send(msgText);
} catch (Exception ex) {
}
}
}, 1, TimeUnit.SECONDS);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
-
Do I need to interpret "about 50 per cent of the messages are lost and never arrive" as "about 50 per cent of the time the onMessage() handler doesn't get fired" ? Or did you actually investigate the queue?Gimby– Gimby2014年06月27日 14:11:31 +00:00Commented Jun 27, 2014 at 14:11
-
1Check if you have a cluster setup, if so, the messages might be going to the other servers queue. You need to setup Messaging bridge in that case to resolve it.Zeus– Zeus2014年06月30日 15:11:17 +00:00Commented Jun 30, 2014 at 15:11
-
I had the same problem if I redeployed my application without shutting down the WebLogic server. I HAVE to follow these steps ... 1) Undeploy the app. 2) Restart the server 3) Redeploy the app. If I don't, I'll only get 50% of my messages. I have code just like yours.Will Lovett– Will Lovett2014年08月26日 17:10:57 +00:00Commented Aug 26, 2014 at 17:10
-
Something else ... you may want to think about setting up a TopicReceiver as opposed to a Queue. The first instance of your SocketMediator will acknowledge the receipt, but I don't think the rest will.Will Lovett– Will Lovett2014年08月26日 17:18:44 +00:00Commented Aug 26, 2014 at 17:18
-
Haha, when I said I have code just like yours. It looks like you found my post: community.oracle.com/thread/3543318Will Lovett– Will Lovett2014年08月26日 17:22:11 +00:00Commented Aug 26, 2014 at 17:22