[jboss-user] [Beginner's Corner] - How can I stop a running mdb?

J Sanz A do-not-reply at jboss.com
Thu Jul 8 17:06:42 EDT 2010


J Sanz A [http://community.jboss.org/people/eskuai] replied to the discussion

"How can I stop a running mdb?"

To view the discussion, visit: http://community.jboss.org/message/551809#551809

--------------------------------------------------------------
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;
        }
    }

--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/551809#551809]

Start a new discussion in Beginner's Corner at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20100708/bee81b03/attachment-0001.html 


More information about the jboss-user mailing list