Jboss messaging

I had to give jboss messaging a go to. Hmm doesn’t look that difficult either. Now a simple MessageDrivenBean and I’m in business.

The Sender:

import java.util.Properties;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JbossJmsTest
{

   public static void main(String[] args) throws Exception
   {
      log.info("Creating jndi context - alternatively use a jndi.properties");
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");
      InitialContext ctx = new InitialContext(properties);

      log.info("Looking up queue");
      Queue queue = (Queue) ctx.lookup("queue/testQueue");

      log.info("Looking up connection factory");
      QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");

      log.info("Creating connection");
      QueueConnection qc = qcf.createQueueConnection();
      try
      {
         log.info("Creating session");
         QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
         
         log.info("Creating sender");
         QueueSender sender = qs.createSender(queue);

         log.info("Creating message");
         TextMessage message = qs.createTextMessage("hello");

         log.info("Sending message");
         sender.send(message);
      }
      finally
      {
         qc.close();
      }
   }

   public static class log
   {
      public static void info(String message)
      {
         System.out.println(message);
      }
      public static void error(String message, Throwable t)
      {
         System.err.println(message);
         t.printStackTrace();
      }
   }
}

The receiver

import java.util.Properties;

import javax.jms.Message;
import javax.jms.Queue;
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.Context;
import javax.naming.InitialContext;

public class JbossJmsReceiver {
	public static void main(String[] args) throws Exception
	   {
	      log.info("Creating jndi context - alternatively use a jndi.properties");
	      Properties properties = new Properties();
	      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
	      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
	      properties.put(Context.PROVIDER_URL, "localhost");
	      InitialContext ctx = new InitialContext(properties);

	      log.info("Looking up queue");
	      Queue queue = (Queue) ctx.lookup("queue/testQueue");

	      log.info("Looking up connection factory");
	      QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");

	      log.info("Creating connection");
	      QueueConnection qc = qcf.createQueueConnection();
	      try
	      {
	         log.info("Creating session");
	         QueueSession qs = qc.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);

	         log.info("Creating receiver");
	         QueueReceiver receiver = qs.createReceiver(queue);

	         log.info("Try to receive message, it will not work");
	         while(true){
		    	 Message received = receiver.receiveNoWait();
		    	 /*if (received != null)
		    		 throw new RuntimeException("Should not get a message if the connection is not started!");*/
		
		    	 //log.info("You have to start the connection before receiving messages");
		    	 qc.start();
		
		    	 //log.info("This receive will work");
		    	 received = receiver.receiveNoWait();
		    	 if(received!=null){
		    		 log.info("Got message: " + received);
		    	 }
	         }
	      }
	      finally
	      {
	         qc.close();
	      }
	   }

	   public static class log
	   {
	      public static void info(String message)
	      {
	         System.out.println(message);
	      }
	      public static void error(String message, Throwable t)
	      {
	         System.err.println(message);
	         t.printStackTrace();
	      }
	   }
}

this is almost the identic code of the jboss site only problem is finding it. It seems jboss has changed the jms server. I’m using jboss-4.0.3SP1 and somewhere they have changed JbossMQ to JBossMessaging or something. I still have to get the details but first things first getting some code working 🙂

First Jabber POC

On my current project I see some real future into messaging. I had some intensive discussions with my manager as he is more pro SOAP webservices then into messaging. But when you have push and pull mechanisms SOAP really isn’t the answer, what would you do? setup 2 SOAP services and let them communicate?? Well what if the specs change and a third party joins in? recode everything and if bla = true connect to X, else connect to Y? Really not!!!
What you can do is place a Queue where you publish everything on. You can use Jboss 4, it has one build in or if you want to go further you could use the Jabber protocol (same as google talk if I’m not mistaken). I have a jabber running on my server and did a very small test in java. I used the smack library, a small native java library. And I wrote this stupid little expample

public static void main(String[] args){
		try {
			XMPPConnection.DEBUG_ENABLED = true;
			SSLXMPPConnection.DEBUG_ENABLED = true;
			XMPPConnection con = new XMPPConnection("pczone.be",5222);
			con.login("xxx", "xxx");
			con.createChat("me").sendMessage("hello");
			PacketFilter filter = new PacketTypeFilter(Message.class);
			PacketCollector myCollector = con.createPacketCollector(filter);
			
			PacketListener myListener = new PacketListener(){
				public void processPacket(Packet pac){
					System.out.println(pac.toString());
				}
			};
			con.addPacketListener(myListener,filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
}

What this stupid little example did was simply logon to my server, send me a message and listen for incoming messages. I ran this in debugmode with a breakpoint on the System.out.println line, when I replied to my message my breakpoint worked. I could inspect the message and see somewhere in the body my content wrapped in a jabber xml.

Off course it’s no rocket science, but it’s always nice when you see something spectaculair. 🙂

I’ll certainly use this in future projects. How exactly, hmm not yet sure 🙂

Java Workshop

Yesterday evening we did an internal workshop. I explained Hibernate a bit Jacob (a collegeau) also shared his experiences on the Hibernate topic as we implemented Hibernate in a totally different way. Afterwards Jacob gave a session on Spring Webflow. I have to say it’s been a while since I went to programmers workshop. Our l2u meetings are not actually coding, more setting up and explaing new stuff. But hey I’m a coder, not an administrator but as most geeks I know my way around administration as well but it’s not my core business 🙂

I spend quite some time preparing the workshop, I even listened to a session of gavin himself recorded on a mp3 player. (I’ll put it up later.) I made a small example and then I did a pojo with 2 relationships to other tables. While preparing this workhop it gave me the opportunity to take a closer look at the things we don’t use in our project. .e.g. we always use HQL to retrieve our objects. Never do we use the Criteria classes way, it comes down to the same thing but it’s more Object oriented then an EJBQL like query 🙂

Although Jacob his session was rather short (due to our enthousiasm) it was really interesting. I had read the spring book, but I can’t remember much from the web flow framework besides it being a continution framework. I have to say it was a lot to take at once but it’s really really nice.
As most of you might know one of the common problems in J2EE web application is that if a user starts at page A, go’s to page B, go’s to page C and then hits the back button 2 times, changes something in form A and submits it. We actually have a merge of page C with page A. This is a problem and this is where a continuation framework comes to the rescue.
It holds some kind of version of the submited paged and if you your back button a couple of times and then submit the serverside object’s state will correspond as if you had never gone any further in your workflow. You actually went back in your workflow.
He also explained that you can define flows. Let’s say you have a OrderFlow,SearchProductFlow and SearchCustomerFlow. You can start your OrderFlow and when you come to the state where the product should get added you leavy your OrderFlow and start a subflow, the SearchProductFlow. Once this flow has finished (and the user has his selected products) we return those products into the OrderFlow and we go on from the point we left it. You then go a couple of steps further and come to the point you have to select a Customer, then you start another subflow, SearchCustomerFlow,… you get the point.

I’m not quite sure, but I believe Ruby on Rails also has this feature. I bet there are more projects out there that do this, but in Java this was rather hard to do. until now offcourse

On another note I was talking to a friend of mine, I told him I was going to give a talk on Hibernate. He told me would have loved to join, but it was an internal workshop. I talked to my boss about this and kinda proposed to make a more open thing. Something like a jug. I know we have Bejug but I haven’t seen much events off them and on their site isn’t much info about them, therefor I would like to ask if any of the readers of this entry who are interested in joining such a jug would send me an email (fix dot pczone dot be). who knows if we can get a small community together we could actually do a session at least once a month. Probably in the future 1 topic per session will be done as 2 topics were really to much 🙂

So Axel or Tom who’s next 😉