3 Replies Latest reply on Jul 8, 2010 5:06 PM by eskuai

    How can I stop a running mdb?

    ramboid

      I have a message driven bean that is running in a long task but I need to stop the process.  i shutdown and restarted jboss but the bean starts running again.

        • 1. Re: How can I stop a running mdb?
          eskuai
          • 2. Re: How can I stop a running mdb?
            ramboid

            This link has nothing

            • 3. Re: How can I stop a running mdb?
              eskuai

              package paq;

               

              import javax.ejb.ActivationConfigProperty;
              import javax.ejb.MessageDriven;
              import javax.jms.JMSException;
              import javax.jms.Message;
              import javax.jms.MessageListener;
              import javax.jms.TextMessage;

               

              /**
              * Message-Driven Bean implementation class for: EjbMDB
              *
              */
              @MessageDriven(
                      activationConfig = {
                              @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                              @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/OrderQueue"),
                              @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
                      },
                      mappedName = "/queue/OrderQueue")
              public class EjbMDB implements MessageListener {

               

                  /**
                   * Default constructor.
                   */
                  public EjbMDB() {
                      // TODO Auto-generated constructor stub
                  }
                 
                  /**
                   * @see MessageListener#onMessage(Message)
                   */
                  public void onMessage(Message message) {
                     
                      System.out.println("----------------");
                      System.out.println("Received message");
                      System.out.println(message);
                      TextMessage tm = (TextMessage) message;
                      try {
                          System.out.println(tm.getText());
                      } catch (JMSException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      System.out.println("----------------");
                     
                  }

               

              }

               

              /*------------------------------------------------------*/

               

              package paq;
              import javax.ejb.Remote;

               

              @Remote
              public interface SessionEJBRemote {
                  public void stopMDB();
                  public void starMDB();
              }

              /*-----------------------------*/

               

              package paq;

               

              import javax.ejb.Stateless;
              import javax.management.MBeanServer;
              import javax.management.MalformedObjectNameException;
              import javax.management.ObjectName;

               

              import org.jboss.ejb3.mdb.MessagingDelegateWrapperMBean;
              import org.jboss.mx.util.MBeanProxy;
              import org.jboss.mx.util.MBeanProxyCreationException;
              import org.jboss.mx.util.MBeanServerLocator;

               

              /**
              * Session Bean implementation class SessionEJB
              */
              @Stateless
              public class SessionEJB implements SessionEJBRemote {

               

                  /**
                   * Default constructor.
                   */
                  public SessionEJB() {
                      // TODO Auto-generated constructor stub
                  }

               

                  public void stopMDB() {
                      String mbean = "jboss.j2ee:jar=StopMDB.jar,name=EjbMDB,service=EJB3";
                 
                      MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
                      MessagingDelegateWrapperMBean invoker;
                      try {
                          invoker = (MessagingDelegateWrapperMBean)
                                     MBeanProxy.get(MessagingDelegateWrapperMBean.class, new ObjectName(mbean),    mbeanServer);
                          invoker.stopDelivery();
                      } catch (MalformedObjectNameException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (MBeanProxyCreationException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (NullPointerException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

               

                      
                     
                     
                  }

               

                  public void starMDB() {
                      String mbean = "jboss.j2ee:jar=StopMDB.jar,name=EjbMDB,service=EJB3";
                     
                      MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
                      MessagingDelegateWrapperMBean invoker;
                      try {
                          invoker = (MessagingDelegateWrapperMBean)
                                     MBeanProxy.get(MessagingDelegateWrapperMBean.class, new ObjectName(mbean),    mbeanServer);
                          invoker.startDelivery();
                      } catch (MalformedObjectNameException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (MBeanProxyCreationException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (NullPointerException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                     
                  }

               

              }

               

               

              /*----------------------------------*/

               

               

              package paq;

               

              import java.util.Properties;

               

              import javax.jms.Connection;
              import javax.jms.ConnectionFactory;
              import javax.jms.Destination;
              import javax.jms.JMSException;
              import javax.jms.MessageProducer;
              import javax.jms.Session;
              import javax.jms.TextMessage;
              import javax.naming.Context;
              import javax.naming.InitialContext;
              import javax.naming.NamingException;

               

               

               

              public class ClientMDB {
                 
              public static void main(String [] args) {
                   //ClientMDB.sendMessage();
                   //ClientMDB.stopMdb();
                   ClientMDB.startMdb();
              }

               

              public static void stopMdb() {
                  ServiceLocator.getSessionEJBRemote().stopMDB();
              }

              public static void startMdb() {
                      ServiceLocator.getSessionEJBRemote().starMDB();
              }
              public static void sendMessage() {
                      try {
                          ConnectionFactory connectionFactory = null;
                          Connection connection = null;
                          Session session = null;
                          Destination destination = null;
                          MessageProducer messageProducer = null;
                          TextMessage message = null;
                         
                          connectionFactory = ServiceLocator.getJmsConnectionFactory("/XAConnectionFactory");
                          connection = connectionFactory.createConnection();
                          session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                          destination = ServiceLocator.getJmsDestination("/queue/OrderQueue");
                          messageProducer = session.createProducer(destination);
                         
                          message = session.createTextMessage("HOLA JUAN");
                          messageProducer.send(message);
                          System.out.println("Message sent to messageProducer");
                          messageProducer.close();
                          session.close();
                          connection.close();

               

                      } catch (JMSException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (Exception e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
              }
              }

               

              /*-----------------------------------*/

               

               

              package paq;

               

              import java.util.Hashtable;

               

              import javax.jms.ConnectionFactory;
              import javax.jms.Destination;
              import javax.naming.Context;
              import javax.naming.InitialContext;
              import javax.naming.NamingException;

               

              public class ServiceLocator {
                  private static Hashtable<String, String> props = new Hashtable<String, String>();
                  static {
                      props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                      props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                      props.put("java.naming.provider.url", "localhost:1099");
                  }
                     private ServiceLocator() {}
                       public static ConnectionFactory getJmsConnectionFactory(String jmsConnectionFactoryJndiName)
                               throws Exception {
                          ConnectionFactory jmsConnectionFactory = null;
                          try {

               

                             
                              Context ctx = new InitialContext(props);
                              jmsConnectionFactory = (ConnectionFactory) ctx.lookup(jmsConnectionFactoryJndiName);
                          } catch (ClassCastException cce) {
                              throw new Exception(cce);
                          } catch (NamingException ne) {
                              throw new  Exception(ne);
                          }
                          return jmsConnectionFactory;
                      }
                      public static Destination getJmsDestination(String jmsDestinationJndiName)
                              throws  Exception {
                          Destination jmsDestination = null;
                          try {
                              Context ctx = new InitialContext(props);
                              jmsDestination = (Destination) ctx.lookup(jmsDestinationJndiName);
                          } catch (ClassCastException cce) {
                              throw new  Exception(cce);
                          } catch (NamingException ne) {
                              throw new  Exception(ne);
                          }
                          return jmsDestination;
                      }
                     
                      public static SessionEJBRemote getSessionEJBRemote() {
                          Context context;
                          SessionEJBRemote remote = null;
                          try {
                              context = new InitialContext(props);
                              remote = (SessionEJBRemote)context.lookup("SessionEJB/remote");
                          } catch (NamingException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                         
                          return remote;
                      }
                  }