6 Replies Latest reply on Dec 28, 2006 7:57 PM by smokingapipe

    Sending Entities in Messages?

    smokingapipe

      I'm new to JMS so please forgive the basicness of this question:

      I have an enterprise application, distributed as an EAR. It uses Seam and EJB3 for most of its work.

      However, there is one Servlet on it which takes input and does some processing. At the end of its processing, it grabs an EntityManager and persists a new entity.

      After that entity has been persisted, it needs to send that entity over to a message queue, where some other things are going to pick up the entity and do stuff with it. That way, the Servlet can finish off the HTTP request immediately, and the processessing (which can take a few minutes) will happen in the background.

      So, here's the question: I can see an easy way to pass the entity ID within a message; I just use a MapMessage and put the integer right into it. Great. Then the MDB can use an entity manager to fetch that entity based on the entity ID. It seems like that would work fine.

      But that also seems clunky. What's the best-practices way to send an entity bean from a Servlet to a MDB?

      Thanks

        • 1. Re: Sending Entities in Messages?
          smokingapipe

          Ok, wait, I just discovered something: the ObjectMessage. I can use that on any class which can be serialized. Fortunately my message classes are very simple things and can be serialized easily.

          But.... this is all running within one JVM. Is JBoss smart enough to know not to serialize these objects, but to just clone them or whatever? I assume it must know this. This is an important performance thing.

          • 2. Re: Sending Entities in Messages?
            smokingapipe

            I realize that JBoss/MQ is dead because JBoss Messaging is going to replace it in the upcoming JBoss 5. Are there any good quick-start guides for doing this simple thing? In a regular Java program, I would create a worker thread, but obviously in an enterprise environment, we don't want to create threads. Instead we send messages. I'm just trying to get a very simple example working, of a servlet sending a message to a MDB within the same EAR.

            • 3. Re: Sending Entities in Messages?
              byorn

              //use this send the object to the queue
              public class SendMessagesViaMDB{

              //method to get the queue connection


              public static javax.jms.QueueConnection getQueueConnection( java.util.Hashtable environment ) throws javax.naming.NamingException, javax.jms.JMSException
              {
              // Obtain initial context
              javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
              try {
              java.lang.Object objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME);
              return ((javax.jms.QueueConnectionFactory) objRef).createQueueConnection();
              } finally {
              initialContext.close();
              }
              }


              /**
              * TO BE CALLED IN THE CLIENT SIDE
              * @param STUFF
              */
              public void sendSTUFFViaMDB(Collection STUFF) {
              QueueConnection queueConnection = null;
              QueueSession jmsSession = null;

              try {
              queueConnection = getTravelAgentMailingServiceMDBQueueConnection();
              jmsSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
              QueueSender sender = jmsSession.createSender(getQueueConnection(JNDI PROPS));

              // create the message and set the content
              ObjectMessage message = jmsSession.createObjectMessage();
              message.setObject((ArrayList) STUFF)

              //send the message
              sender.send(message);
              sender.close();
              log.debug("Message is written to jms queue");
              } catch (JMSException e) {
              log.error("Sending message via jms failed", e);
              } catch (NamingException e) {
              log.error("Sending message via jms failed",e);
              } catch(ModuleException moduleException) {
              log.error("Sending message via jms failed", moduleException);
              } finally {
              if (queueConnection != null){
              try {
              queueConnection.close();
              } catch (JMSException e) {
              log.error("Closing jms queue connection failed", e);
              }
              }
              if (jmsSession != null){
              try {
              jmsSession.close();
              } catch (JMSException e) {
              log.error("Closing jms session failed", e);
              }
              }
              }
              }



              //THE MDB
              public class MessagingServiceMDB implements MessageDrivenBean, MessageListener {

              public void onMessage(Message message){
              try {
              ObjectMessage objMessage = (ObjectMessage)message;
              ArrayList msgProfiles = (ArrayList)objMessage.getObject();
              MyMessageManager manager = new MyMessageManager ();
              manager.processMessages(msgProfiles);
              } catch (JMSException jmsException) {
              + jmsException.getMessage(), jmsException);
              } catch (Exception exception) {
              + exception.getMessage(), exception);
              }
              }

              • 4. Re: Sending Entities in Messages?
                smokingapipe

                I have all this set up, and I'm getting:

                javax.naming.NameNotFoundException: TestQueue not bound
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
                 at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
                 at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
                 at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
                 at javax.naming.InitialContext.lookup(InitialContext.java:392)
                etc
                


                Here's my MDB:

                @MessageDriven(activationConfig =
                {
                 @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
                 @ActivationConfigProperty(propertyName="destination",propertyValue="queue/TestQueue")
                })
                public class TestMDB implements MessageListener {


                which should create teh TestQueue, right?


                Any ideas on this? As with all things JBoss, getting the configuration right is half the battle.

                Thanks


                • 5. Re: Sending Entities in Messages?
                  smokingapipe

                  I just now looked at the JMX console and I didn't see my queue/TestQueue anywhere in it, so obviously JBoss isn't registering that MDB. Hmmm.

                  • 6. Re: Sending Entities in Messages?
                    smokingapipe

                    Ah, never mind that one. My MDB wasn't in my beans jar, and even JBoss isn't psychic, so...