Breaking News
Home / ServerSide / Snoop MDB Java

Snoop MDB Java

Snoop MDB

The message driven beans are consumers of JMS messages. When message is received from the queue, we can find out a lot of details about the message. The following snippet of MDB tries to find out like name, id , destination , type etc.

<!>

Snippet

  1. import java.util.*;
  2. import javax.ejb.*;
  3. import javax.jms.*;
  4. import javax.naming.*;
  5. public class SnoopMDB implements MessageDrivenBean, MessageListener
  6. {
  7.   private MessageDrivenContext context;
  8.   private Context jndiContext;
  9.   public void ejbCreate () throws CreateException {}
  10.   public void ejbRemove() {}
  11.   public void setMessageDrivenContext(MessageDrivenContext ctx)
  12.   {
  13.     context = ctx;
  14.     try
  15.     {
  16.       jndiContext = new InitialContext();
  17.     }
  18.     catch(NamingException ne)
  19.     {
  20.       throw new EJBException(ne);
  21.     }
  22.   }
  23.   /**
  24.    * The business logic
  25.    */
  26.   public void onMessage(Message msg)
  27.   {
  28.     try
  29.     {
  30.       String text = null;
  31.       System.out.println(“===== New Message =====”);
  32.       System.out.println(“Type:        “ + msg.getClass().getName());
  33.       System.out.println(“Destination: “ + msg.getJMSDestination());
  34.       System.out.println(“Message ID:  “ + msg.getJMSMessageID());
  35.       if ( msg instanceof TextMessage )
  36.       {
  37.         text = ((TextMessage)msg).getText();
  38.       }
  39.       else if ( msg instanceof ObjectMessage )
  40.       {
  41.         text = ((ObjectMessage)msg).getObject().toString();
  42.       }
  43.       else if ( msg instanceof MapMessage )
  44.       {
  45.         StringBuffer sb = new StringBuffer(n);
  46.         MapMessage mm = (MapMessage)msg;
  47.         Enumeration enum = mm.getMapNames();
  48.         while (enum.hasMoreElements())
  49.         {
  50.           String name = (String)enum.nextElement();
  51.           Object val = mm.getObject(name);
  52.           sb.append(“> “ + name + ” = “ + val + n);
  53.         }
  54.         text = sb.toString();
  55.       }
  56.       if (text != null)
  57.         System.out.println(“Content:     “ + text);
  58.     }
  59.     catch(Throwable t)
  60.     {
  61.       System.out.println(“?! Failed:   “ + t.getMessage());
  62.     }
  63.   }
  64. }
[pb_builder]

Check Also

What webservices standards are supported by Siebel Business Applications?

The following Web services standards are supported by Siebel Business Applications:     * Web Services …

Advertisment ad adsense adlogger