Preloader image

Declarando um @ApplicationException

    import javax.ejb.ApplicationException;

    /**
     * @version $Rev: 784112 $ $Date: 2009-06-12 06:23:57 -0700 (Fri, 12 Jun 2009) $
     */
    @ApplicationException(rollback = true)
    public abstract class BusinessException extends RuntimeException {
    }

por padrão, @ApplicationException é herdado

    public class ValueRequiredException extends BusinessException {
    }

No código do bean

    @Stateless
    public class ThrowBusinessExceptionImpl implements ThrowBusinessException {

        public void throwValueRequiredException() throws BusinessException {
            throw new ValueRequiredException();
        }

    }

Normalmente lançando um RuntimeException que faria o contêiner reverter a transação e destruir a instância do bean que lançou a exceção.

Como a BusinessException foi anotada com o @ApplicationException(rollback = true) somente a transação de rollback ocorrerá e o bean não será destruído.

Caso de Teste

    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import java.util.Properties;

    public class ThrowBusinessExceptionImplTest {

        private InitialContext initialContext;

        @Before
        public void setUp() throws Exception {
            Properties properties = new Properties();
            properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");

            initialContext = new InitialContext(properties);
        }

        @Test(expected = ValueRequiredException.class)
        public void testCounterViaRemoteInterface() throws Exception {
            Object object = initialContext.lookup("ThrowBusinessExceptionImplRemote");

            Assert.assertNotNull(object);
            Assert.assertTrue(object instanceof ThrowBusinessException);
            ThrowBusinessException bean = (ThrowBusinessException) object;
            bean.throwValueRequiredException();
        }
    }

Executando

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running org.superbiz.appexception.ThrowBusinessExceptionImplTest
    Apache OpenEJB 4.0.0-beta-1    build: 20111002-04:06
    http://tomee.apache.org/
    INFO - openejb.home = /Users/dblevins/examples/applicationexception
    INFO - openejb.base = /Users/dblevins/examples/applicationexception
    INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
    INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
    INFO - Found EjbModule in classpath: /Users/dblevins/examples/applicationexception/target/classes
    INFO - Beginning load: /Users/dblevins/examples/applicationexception/target/classes
    INFO - Configuring enterprise application: /Users/dblevins/examples/applicationexception/classpath.ear
    INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
    INFO - Auto-creating a container for bean ThrowBusinessExceptionImpl: Container(type=STATELESS, id=Default Stateless Container)
    INFO - Enterprise application "/Users/dblevins/examples/applicationexception/classpath.ear" loaded.
    INFO - Assembling app: /Users/dblevins/examples/applicationexception/classpath.ear
    INFO - Jndi(name=ThrowBusinessExceptionImplRemote) --> Ejb(deployment-id=ThrowBusinessExceptionImpl)
    INFO - Jndi(name=global/classpath.ear/applicationexception/ThrowBusinessExceptionImpl!org.superbiz.appexception.ThrowBusinessException) --> Ejb(deployment-id=ThrowBusinessExceptionImpl)
    INFO - Jndi(name=global/classpath.ear/applicationexception/ThrowBusinessExceptionImpl) --> Ejb(deployment-id=ThrowBusinessExceptionImpl)
    INFO - Created Ejb(deployment-id=ThrowBusinessExceptionImpl, ejb-name=ThrowBusinessExceptionImpl, container=Default Stateless Container)
    INFO - Started Ejb(deployment-id=ThrowBusinessExceptionImpl, ejb-name=ThrowBusinessExceptionImpl, container=Default Stateless Container)
    INFO - Deployed Application(path=/Users/dblevins/examples/applicationexception/classpath.ear)
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.434 sec

    Results :

    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0