java - Messages get lost on WebLogic WebSocket connection -
i implemented jms client supposed update browser when jms message arrives. unfortunately, 50 per cent of messages lost , never arrive. strange. reason?
here post part of code. created client according html5 websocket examples weblogic , have version 12.1.2. have no errors on java nor on javascript side. console prints receive message bean: shit happens ist printed every 2 seconds, receive on client every 4 seconds or less. why 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 threadscheduledexecutor invoke runnable @ later time // posts data 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 created client " + websocketconnection.getremoteaddress()); super.onopen(websocketconnection); } @override public void onmessage(websocketconnection connection, string payload) { // sends message browser client. system.out.println("does ever fire?"); } @override public void onclose(websocketconnection websocketconnection, closingmessage closingmessage) { super.onclose(websocketconnection, closingmessage); system.out.println("connection closed client " + websocketconnection.getremoteaddress()); } @override public void onerror(websocketconnection websocketconnection, throwable throwable) { super.onerror(websocketconnection, throwable); system.out.println("something went wrong client " + websocketconnection.getremoteaddress()); } // subscription 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 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(); } } }
Comments
Post a Comment