Permalink
Browse files

SessionListener / MessageListener structure.

  • Loading branch information...
1 parent cca7694 commit a50c9975e9dbce61d516df781fe48dce0979d1c3 ckaminsk committed Jun 21, 2012
@@ -0,0 +1,15 @@
+package com.darthcoder.JMSTestServlet;
+
+import javax.jms.Message;
+import javax.jms.MessageListener;
+
+public class JMSSession implements MessageListener {
+
+
+ @Override
+ public void onMessage(Message message) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
@@ -0,0 +1,35 @@
+package com.darthcoder.JMSTestServlet;
+
+import javax.servlet.http.HttpSessionBindingEvent;
+import javax.servlet.http.HttpSessionBindingListener;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+public class JMSSessionListener implements HttpSessionListener,
+ HttpSessionBindingListener {
+
+ @Override
+ public void valueBound(HttpSessionBindingEvent event) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void valueUnbound(HttpSessionBindingEvent event) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void sessionCreated(HttpSessionEvent se) {
+ System.out.println("Session created!");
+
+ }
+
+ @Override
+ public void sessionDestroyed(HttpSessionEvent se) {
+ System.out.println("Session destroyed!");
+
+ }
+
+}
@@ -1,10 +1,14 @@
package com.darthcoder.JMSTestServlet;
import java.io.IOException;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
+import javax.servlet.ServletContext;
import javax.servlet.http.*;
//import com.sun.xml.rpc.processor.modeler.j2ee.xml.javaXmlTypeMappingType;
@@ -13,6 +17,11 @@
public class MainServlet extends HttpServlet {
boolean stopFlag = false;
+ @Override
+ public void init() {
+
+ }
+
private String getJMSNames() {
StringBuilder retVal = new StringBuilder();
try {
@@ -38,6 +47,41 @@ private String getJMSNames() {
return retVal.toString();
}
+ void addPrimerMessage() {
+ // Adds a primer message to the ReplyTo queue.
+ System.out.println("Adding primer message to the message queue - to test R&D");
+ QueueConnection queueConnection = null;
+ try {
+
+ InitialContext ctx = new InitialContext();
+ QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)ctx.lookup("jms/QueueFactory1");
+ Queue queue = (Queue)ctx.lookup("jms/ReplyTo");
+
+ queueConnection = queueConnectionFactory.createQueueConnection();
+ queueConnection.start();
+ QueueSession queueSession = queueConnection.createQueueSession(false,
+ Session.AUTO_ACKNOWLEDGE);
+ QueueSender sender = queueSession.createSender(queue);
+
+ TextMessage msg = queueSession.createTextMessage();
+ msg.setText("A message from MessageServlet");
+ msg.setStringProperty("name", "MessageServlet");
+
+ sender.send(msg);
+ } catch (JMSException e) {
+ throw new RuntimeException(e);
+ } catch (NamingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally {
+ try {
+ if (queueConnection != null) {
+ queueConnection.close();
+ }
+ } catch (JMSException e) { //ignore
+ }
+ }
+ }
// TODO: Add authentication capabilities.
void runMessageListener(HttpServletRequest request, HttpServletResponse response) {
System.out.println("Starting runMessageListener");
@@ -65,7 +109,9 @@ void runMessageListener(HttpServletRequest request, HttpServletResponse response
while ( !stopFlag ) {
// TODO: wait on exit flag
// TODO: make this a user-specifiable parameter.
- Message msg = consumer.receive(1000);
+ TextMessage msg = (TextMessage) consumer.receive(1000);
+ if ( msg != null )
+ System.out.println("Message: " + msg.toString());
}
conn.stop();
@@ -94,8 +140,11 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
}
} else if (action.equals("jmsmessages") ) {
runMessageListener(request, response);
+ } else if (action.equals("primermessage")) {
+ addPrimerMessage();
} else {
}
}
}
+
@@ -3,15 +3,20 @@
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
- <display-name>JMS Test Applet</display-name>
- <servlet>
- <servlet-name>JMS Test Applet</servlet-name>
- <servlet-class>com.darthcoder.JMSTestServlet.MainServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
+ <display-name>JMS Test Applet</display-name>
- <servlet-mapping>
- <servlet-name>JMS Test Applet</servlet-name>
- <url-pattern>/jmsops/*</url-pattern>
- </servlet-mapping>
+ <listener>
+ <listener-class>com.darthcoder.JMSTestServlet.JMSSessionListener</listener-class>
+ </listener>
+
+ <servlet>
+ <servlet-name>JMS Test Applet</servlet-name>
+ <servlet-class>com.darthcoder.JMSTestServlet.MainServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>JMS Test Applet</servlet-name>
+ <url-pattern>/jmsops/*</url-pattern>
+ </servlet-mapping>
</web-app>
@@ -20,7 +20,7 @@
});
function startListener() {
- var argumentStr = $('#CFactory').val() + '&' + $('#ReplyTo').val();
+ var argumentStr = 'ConnFactoryName=' + $('#CFactory').val() + '&ReplyToName=' + $('#ReplyTo').val();
var urlString = "./jmsops/?action=jmsmessages&" + argumentStr;
alert("Using URLString " + urlString);
$.ajax({ url: urlString, success: function(data){

0 comments on commit a50c997

Please sign in to comment.