Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * JBoss, Home of Professional Open Source
   * Copyright 2005, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site:
 package org.jbpm.ejb;
 import javax.ejb.EJB;
 import  javax.jms.Connection;
 import  javax.jms.ConnectionFactory;
 import  javax.jms.Destination;
 import  javax.jms.JMSException;
 import  javax.jms.Message;
 import  javax.jms.MessageListener;
 import  javax.jms.MessageProducer;
 import  javax.jms.ObjectMessage;
 import  javax.jms.Session;
This message-driven bean listens for object messages containing a command instance. The received commands are executed by the command service bean, using the local interface. The body of the message must be a Java object that implements the Command interface. The message properties, if any, are ignored.


The environment entries and resources available for customization are summarized in the table below.

ejb/LocalCommandServiceEJB ReferenceLink to the local session bean that executes commands on a separate jBPM context.
jms/JbpmConnectionFactoryResource Manager ReferenceLogical name of the factory that provides JMS connections for producing result messages. Required for command messages that indicate a reply destination.

Alejandro Guizar
 public class CommandListenerBean implements MessageListener {
   private static final long serialVersionUID = 1L;
   @EJB(name = "ejb/LocalCommandService")
   @Resource(name = "jms/JbpmConnectionFactory", shareable = true)
   private ConnectionFactory jmsConnectionFactory;
   private static final Log log = LogFactory.getLog(CommandListenerBean.class);
   public void onMessage(Message message) {
     try {
       // extract command from message
      Command command = extractCommand(message);
      // a null return value means the message did not carry a valid command
      // warnings were logged already; just swallow the message and return 
      if (command == nullreturn;
      // execute command via local command executor bean
      Object result;
      try {
        if (.isDebugEnabled()) {
          .debug("executing " + command);
        result = .execute(command);
        if (.isTraceEnabled()) {
          .trace(command + " completed successfully, committing");
      catch (RuntimeException e) {
        // if this is a locking exception, keep it quiet
        if (DbPersistenceService.isLockingException(e)) {
            + " failed to execute " + commande);
        else {
          .error(message + " failed to execute " + commande);
        // MDBs are not supposed to throw exceptions
      // send a response back if a "reply to" destination is set
      Destination replyTo;
      if ( != null && (replyTo = message.getJMSReplyTo()) != null
        && (result instanceof Serializable || result == null)) {
    catch (JMSException e) {
      .error("failed to process message " + messagee);

Retrieves a Command instance from the given message, which is assumed to be an ObjectMessage.

Subclasses may override this method to materialize the command in some other way.

  protected Command extractCommand(Message messagethrows JMSException {
    if (message instanceof ObjectMessage) {
      ObjectMessage objectMessage = (ObjectMessage) message;
      Serializable object = objectMessage.getObject();
      if (object instanceof Command) {
        return (Commandobject;
      else {
        .warn(object + " is not a command");
    else {
      .warn(message + " is not an object message");
    return null;
  private void sendResult(Serializable result, Destination destinationString correlationId)
    throws JMSException {
    if (.isDebugEnabled()) .debug("sending " + result + " to " + destination);
    Connection jmsConnection = .createConnection();
    try {
       * if the connection supports xa, the session will be transacted, else the session will
       * auto acknowledge; in either case no explicit transaction control must be performed -
       * see ejb 2.1 - 17.3.5
      Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Message resultMessage = jmsSession.createObjectMessage(result);
      MessageProducer producer = jmsSession.createProducer(destination);
    finally {
      // there is no need to close the sessions and producers of a closed connection
      try {
      catch (JMSException e) {
        .warn("failed to close jms connection"e);
New to GrepCode? Check out our FAQ X