Search in sources :

Example 6 with StatelessEJBLocator

use of org.jboss.ejb.client.StatelessEJBLocator in project wildfly by wildfly.

the class EJBClientUserTransactionTestCase method testMandatoryTxOnSLSB.

/**
     * Tests a call to a bean method with a Mandatory tx attribute, by initiating a UserTransaction on the
     * remote client side. This test ensures that the tx is propagated to the server during the bean invocation
     *
     * @throws Exception
     */
@Test
public void testMandatoryTxOnSLSB() throws Exception {
    final StatelessEJBLocator<CMTRemote> cmtRemoteBeanLocator = new StatelessEJBLocator<CMTRemote>(CMTRemote.class, APP_NAME, MODULE_NAME, CMTBean.class.getSimpleName(), "");
    final CMTRemote cmtRemoteBean = EJBClient.createProxy(cmtRemoteBeanLocator);
    final UserTransaction userTransaction = EJBClient.getUserTransaction(nodeName);
    userTransaction.begin();
    cmtRemoteBean.mandatoryTxOp();
    userTransaction.commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) StatelessEJBLocator(org.jboss.ejb.client.StatelessEJBLocator) Test(org.junit.Test)

Example 7 with StatelessEJBLocator

use of org.jboss.ejb.client.StatelessEJBLocator in project wildfly by wildfly.

the class EJBClientUserTransactionTestCase method testServerSuspension.

/**
     * Calls for a preexistent transaction are allowed and calls for a non-preexistent transaction are not allowed
     * on server suspension.
     *
     * @throws Exception
     */
@Test
public void testServerSuspension() throws Exception {
    final StatelessEJBLocator<CMTRemote> cmtRemoteBeanLocator = new StatelessEJBLocator<CMTRemote>(CMTRemote.class, APP_NAME, MODULE_NAME, CMTBean.class.getSimpleName(), "");
    final CMTRemote cmtRemoteBean = EJBClient.createProxy(cmtRemoteBeanLocator);
    // begin the transaction
    UserTransaction userTransaction = EJBClient.getUserTransaction(nodeName);
    userTransaction.begin();
    try {
        // invoke the bean
        cmtRemoteBean.mandatoryTxOp();
        ModelNode op = new ModelNode();
        op.get(OP).set("suspend");
        managementClient.getControllerClient().execute(op);
        userTransaction.commit();
    } catch (Exception e) {
        try {
            userTransaction.rollback();
        } catch (Exception exc) {
        }
        throw e;
    } finally {
        // resume server
        ModelNode op = new ModelNode();
        op.get(OP).set("resume");
        managementClient.getControllerClient().execute(op);
    }
    try {
        // begin a transaction
        userTransaction.begin();
        long fin = System.currentTimeMillis() + TimeoutUtil.adjust(5000);
        while (true) {
            try {
                // can invoke bean
                cmtRemoteBean.mandatoryTxOp();
                break;
            } catch (Exception e) {
                if (System.currentTimeMillis() > fin) {
                    throw e;
                }
            }
            Thread.sleep(300);
        }
        // suspend server
        ModelNode op = new ModelNode();
        op.get(OP).set("suspend");
        managementClient.getControllerClient().execute(op);
        // can continue invoking bean with current transaction
        cmtRemoteBean.mandatoryTxOp();
    } catch (Exception e) {
        e.printStackTrace();
        // resume server
        ModelNode op = new ModelNode();
        op.get(OP).set("resume");
        managementClient.getControllerClient().execute(op);
        throw e;
    } finally {
        // rollback current transaction
        userTransaction.commit();
    }
    // still cannot begin a new transaction
    userTransaction.begin();
    try {
        cmtRemoteBean.mandatoryTxOp();
        Assert.fail("Exception expected, server is shutdown");
    } catch (Exception expected) {
    // expected
    } finally {
        userTransaction.rollback();
    }
    // resume server
    ModelNode op = new ModelNode();
    op.get(OP).set("resume");
    managementClient.getControllerClient().execute(op);
    try {
        // begin a transaction, and make sure that the server now works normally
        userTransaction.begin();
        long fin = System.currentTimeMillis() + TimeoutUtil.adjust(5000);
        while (true) {
            try {
                // can invoke bean
                cmtRemoteBean.mandatoryTxOp();
                break;
            } catch (Exception e) {
                if (System.currentTimeMillis() > fin) {
                    throw e;
                }
            }
            Thread.sleep(300);
        }
        // end the tx
        userTransaction.commit();
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) StatelessEJBLocator(org.jboss.ejb.client.StatelessEJBLocator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 8 with StatelessEJBLocator

use of org.jboss.ejb.client.StatelessEJBLocator in project wildfly by wildfly.

the class EJBClientUserTransactionTestCase method testBatchOperationsInTx.

/**
     * Tests various possibilities with a user initiated UserTransaction and subsequent bean invocations
     *
     * @throws Exception
     */
@Test
public void testBatchOperationsInTx() throws Exception {
    final StatelessEJBLocator<RemoteBatch> batchBeanLocator = new StatelessEJBLocator<RemoteBatch>(RemoteBatch.class, APP_NAME, MODULE_NAME, BatchCreationBean.class.getSimpleName(), "");
    final RemoteBatch batchBean = EJBClient.createProxy(batchBeanLocator);
    final StatelessEJBLocator<BatchRetriever> batchRetrieverLocator = new StatelessEJBLocator<BatchRetriever>(BatchRetriever.class, APP_NAME, MODULE_NAME, BatchFetchingBean.class.getSimpleName(), "");
    final BatchRetriever batchRetriever = EJBClient.createProxy(batchRetrieverLocator);
    final UserTransaction userTransaction = EJBClient.getUserTransaction(nodeName);
    final String batchName = "Simple Batch";
    // create a batch
    userTransaction.begin();
    try {
        batchBean.createBatch(batchName);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    userTransaction.commit();
    // add step1 to the batch
    final String step1 = "Simple step1";
    userTransaction.begin();
    try {
        batchBean.step1(batchName, step1);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    userTransaction.commit();
    String successFullyCompletedSteps = step1;
    // fetch the batch and make sure it contains the right state
    final Batch batchAfterStep1 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step1 was null", batchAfterStep1);
    Assert.assertEquals("Unexpected steps in batch, after step1", successFullyCompletedSteps, batchAfterStep1.getStepNames());
    // now add a failing step2
    final String appExceptionStep2 = "App exception Step 2";
    userTransaction.begin();
    try {
        batchBean.appExceptionFailingStep2(batchName, appExceptionStep2);
        Assert.fail("Expected an application exception");
    } catch (SimpleAppException sae) {
        // expected
        userTransaction.rollback();
    }
    final Batch batchAfterAppExceptionStep2 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after app exception step2 was null", batchAfterAppExceptionStep2);
    Assert.assertEquals("Unexpected steps in batch, after app exception step2", successFullyCompletedSteps, batchAfterAppExceptionStep2.getStepNames());
    // now add a successful step2
    final String step2 = "Simple Step 2";
    userTransaction.begin();
    try {
        batchBean.successfulStep2(batchName, step2);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    // don't yet commit and try and retrieve the batch
    final Batch batchAfterStep2BeforeCommit = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step2, before commit was null", batchAfterStep2BeforeCommit);
    Assert.assertEquals("Unexpected steps in batch, after step2 before commit", successFullyCompletedSteps, batchAfterStep2BeforeCommit.getStepNames());
    // now commit
    userTransaction.commit();
    // keep track of successfully completely steps
    successFullyCompletedSteps = successFullyCompletedSteps + "," + step2;
    // now retrieve and check the batch
    final Batch batchAfterStep2 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step2 was null", batchAfterStep2);
    Assert.assertEquals("Unexpected steps in batch, after step2", successFullyCompletedSteps, batchAfterStep2.getStepNames());
    // now add independent Step3 (i.e. the bean method has a REQUIRES_NEW semantics, so that the
    // client side tx doesn't play a role)
    final String step3 = "Simple Step 3";
    userTransaction.begin();
    batchBean.independentStep3(batchName, step3);
    // rollback (but it shouldn't end up rolling back step3 because that was done in server side independent tx)
    userTransaction.rollback();
    // keep track of successfully completely steps
    successFullyCompletedSteps = successFullyCompletedSteps + "," + step3;
    // now retrieve and check the batch
    final Batch batchAfterStep3 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step3 was null", batchAfterStep3);
    Assert.assertEquals("Unexpected steps in batch, after step3", successFullyCompletedSteps, batchAfterStep3.getStepNames());
    // now add step4 but don't commit
    final String step4 = "Simple Step 4";
    userTransaction.begin();
    batchBean.step4(batchName, step4);
    // now add a system exception throwing step
    final String sysExceptionStep2 = "Sys exception step2";
    try {
        batchBean.systemExceptionFailingStep2(batchName, sysExceptionStep2);
        Assert.fail("Expected a system exception");
    } catch (Exception e) {
        // expected exception
        // TODO: We currently don't return the tx status from the server to the client, so the
        // client has no knowledge of the tx status. This is something that can be implemented
        // by passing along the tx status as a return attachment from a remote method invocation.
        // For now, let's ignore it
        //Assert.assertEquals("Unexpected transaction state", Status.STATUS_ROLLEDBACK, userTransaction.getStatus());
        userTransaction.rollback();
    }
    // now retrieve and check the batch
    final Batch batchAfterSysException = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after system exception was null", batchAfterSysException);
    Assert.assertEquals("Unexpected steps in batch, after system exception", successFullyCompletedSteps, batchAfterSysException.getStepNames());
}
Also used : UserTransaction(javax.transaction.UserTransaction) StatelessEJBLocator(org.jboss.ejb.client.StatelessEJBLocator) Test(org.junit.Test)

Example 9 with StatelessEJBLocator

use of org.jboss.ejb.client.StatelessEJBLocator in project wildfly by wildfly.

the class EJBClientXidTransactionTestCase method testServerSuspension.

/**
     * Calls for a preexistent transaction are allowed and calls for a non-preexistent transaction are not allowed
     * on server suspension.
     *
     * @throws Exception
     */
@Test
public void testServerSuspension() throws Exception {
    final StatelessEJBLocator<org.jboss.as.test.integration.ejb.remote.client.api.tx.CMTRemote> cmtRemoteBeanLocator = new StatelessEJBLocator<org.jboss.as.test.integration.ejb.remote.client.api.tx.CMTRemote>(org.jboss.as.test.integration.ejb.remote.client.api.tx.CMTRemote.class, APP_NAME, MODULE_NAME, org.jboss.as.test.integration.ejb.remote.client.api.tx.CMTBean.class.getSimpleName(), "");
    final org.jboss.as.test.integration.ejb.remote.client.api.tx.CMTRemote cmtRemoteBean = EJBClient.createProxy(cmtRemoteBeanLocator);
    // begin a transaction, and make sure that the server now works normally
    txManager.begin();
    try {
        // invoke the bean
        cmtRemoteBean.mandatoryTxOp();
    } finally {
        // end the tx
        txManager.commit();
    }
    // begin the transaction
    txManager.begin();
    try {
        // invoke the bean
        cmtRemoteBean.mandatoryTxOp();
        ModelNode op = new ModelNode();
        op.get(OP).set("suspend");
        managementClient.getControllerClient().execute(op);
        txManager.commit();
    } catch (Exception e) {
        try {
            txManager.rollback();
        } catch (Exception exc) {
        }
        throw e;
    } finally {
        // resume server
        ModelNode op = new ModelNode();
        op.get(OP).set("resume");
        managementClient.getControllerClient().execute(op);
    }
    try {
        // begin a transaction
        txManager.begin();
        long fin = System.currentTimeMillis() + TimeoutUtil.adjust(5000);
        while (true) {
            try {
                // can invoke bean
                cmtRemoteBean.mandatoryTxOp();
                break;
            } catch (Exception e) {
                if (System.currentTimeMillis() > fin) {
                    throw e;
                }
            }
            Thread.sleep(300);
        }
        // suspend server
        ModelNode op = new ModelNode();
        op.get(OP).set("suspend");
        managementClient.getControllerClient().execute(op);
        // can continue invoking bean with current transaction
        cmtRemoteBean.mandatoryTxOp();
    } catch (Exception e) {
        e.printStackTrace();
        // resume server
        ModelNode op = new ModelNode();
        op.get(OP).set("resume");
        managementClient.getControllerClient().execute(op);
        throw e;
    } finally {
        // rollback current transaction
        txManager.commit();
    }
    // still cannot begin a new transaction
    txManager.begin();
    try {
        cmtRemoteBean.mandatoryTxOp();
        Assert.fail("Exception expected, server is shutdown");
    } catch (Exception expected) {
    // expected
    } finally {
        txManager.rollback();
    }
    // resume server
    ModelNode op = new ModelNode();
    op.get(OP).set("resume");
    managementClient.getControllerClient().execute(op);
    try {
        // begin a transaction, and make sure that the server now works normally
        txManager.begin();
        long fin = System.currentTimeMillis() + TimeoutUtil.adjust(5000);
        while (true) {
            try {
                // can invoke bean
                cmtRemoteBean.mandatoryTxOp();
                break;
            } catch (Exception e) {
                if (System.currentTimeMillis() > fin) {
                    throw e;
                }
            }
            Thread.sleep(300);
        }
        // end the tx
        txManager.commit();
    } catch (Exception e) {
        txManager.rollback();
        throw e;
    }
}
Also used : StatelessEJBLocator(org.jboss.ejb.client.StatelessEJBLocator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 10 with StatelessEJBLocator

use of org.jboss.ejb.client.StatelessEJBLocator in project wildfly by wildfly.

the class HTTPEJBClientUserTransactionTestCase method testBatchOperationsInTx.

/**
     * Tests various possibilities with a user initiated UserTransaction and subsequent bean invocations
     *
     * @throws Exception
     */
@Test
public void testBatchOperationsInTx() throws Exception {
    final StatelessEJBLocator<RemoteBatch> batchBeanLocator = new StatelessEJBLocator<RemoteBatch>(RemoteBatch.class, APP_NAME, MODULE_NAME, BatchCreationBean.class.getSimpleName(), "", Affinity.forUri(getHttpUri()));
    final RemoteBatch batchBean = EJBClient.createProxy(batchBeanLocator);
    final StatelessEJBLocator<BatchRetriever> batchRetrieverLocator = new StatelessEJBLocator<BatchRetriever>(BatchRetriever.class, APP_NAME, MODULE_NAME, BatchFetchingBean.class.getSimpleName(), "", Affinity.forUri(getHttpUri()));
    final BatchRetriever batchRetriever = EJBClient.createProxy(batchRetrieverLocator);
    final UserTransaction userTransaction = getUserTransaction();
    final String batchName = "Simple Batch";
    // create a batch
    userTransaction.begin();
    try {
        batchBean.createBatch(batchName);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    userTransaction.commit();
    // add step1 to the batch
    final String step1 = "Simple step1";
    userTransaction.begin();
    try {
        batchBean.step1(batchName, step1);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    userTransaction.commit();
    String successFullyCompletedSteps = step1;
    // fetch the batch and make sure it contains the right state
    final Batch batchAfterStep1 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step1 was null", batchAfterStep1);
    Assert.assertEquals("Unexpected steps in batch, after step1", successFullyCompletedSteps, batchAfterStep1.getStepNames());
    // now add a failing step2
    final String appExceptionStep2 = "App exception Step 2";
    userTransaction.begin();
    try {
        batchBean.appExceptionFailingStep2(batchName, appExceptionStep2);
        Assert.fail("Expected an application exception");
    } catch (SimpleAppException sae) {
        // expected
        userTransaction.rollback();
    }
    final Batch batchAfterAppExceptionStep2 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after app exception step2 was null", batchAfterAppExceptionStep2);
    Assert.assertEquals("Unexpected steps in batch, after app exception step2", successFullyCompletedSteps, batchAfterAppExceptionStep2.getStepNames());
    // now add a successful step2
    final String step2 = "Simple Step 2";
    userTransaction.begin();
    try {
        batchBean.successfulStep2(batchName, step2);
    } catch (Exception e) {
        userTransaction.rollback();
        throw e;
    }
    // don't yet commit and try and retrieve the batch
    final Batch batchAfterStep2BeforeCommit = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step2, before commit was null", batchAfterStep2BeforeCommit);
    Assert.assertEquals("Unexpected steps in batch, after step2 before commit", successFullyCompletedSteps, batchAfterStep2BeforeCommit.getStepNames());
    // now commit
    userTransaction.commit();
    // keep track of successfully completely steps
    successFullyCompletedSteps = successFullyCompletedSteps + "," + step2;
    // now retrieve and check the batch
    final Batch batchAfterStep2 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step2 was null", batchAfterStep2);
    Assert.assertEquals("Unexpected steps in batch, after step2", successFullyCompletedSteps, batchAfterStep2.getStepNames());
    // now add independent Step3 (i.e. the bean method has a REQUIRES_NEW semantics, so that the
    // client side tx doesn't play a role)
    final String step3 = "Simple Step 3";
    userTransaction.begin();
    batchBean.independentStep3(batchName, step3);
    // rollback (but it shouldn't end up rolling back step3 because that was done in server side independent tx)
    userTransaction.rollback();
    // keep track of successfully completely steps
    successFullyCompletedSteps = successFullyCompletedSteps + "," + step3;
    // now retrieve and check the batch
    final Batch batchAfterStep3 = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after step3 was null", batchAfterStep3);
    Assert.assertEquals("Unexpected steps in batch, after step3", successFullyCompletedSteps, batchAfterStep3.getStepNames());
    // now add step4 but don't commit
    final String step4 = "Simple Step 4";
    userTransaction.begin();
    batchBean.step4(batchName, step4);
    // now add a system exception throwing step
    final String sysExceptionStep2 = "Sys exception step2";
    try {
        batchBean.systemExceptionFailingStep2(batchName, sysExceptionStep2);
        Assert.fail("Expected a system exception");
    } catch (Exception e) {
        // expected exception
        // TODO: We currently don't return the tx status from the server to the client, so the
        // client has no knowledge of the tx status. This is something that can be implemented
        // by passing along the tx status as a return attachment from a remote method invocation.
        // For now, let's ignore it
        //Assert.assertEquals("Unexpected transaction state", Status.STATUS_ROLLEDBACK, userTransaction.getStatus());
        userTransaction.rollback();
    }
    // now retrieve and check the batch
    final Batch batchAfterSysException = batchRetriever.fetchBatch(batchName);
    Assert.assertNotNull("Batch after system exception was null", batchAfterSysException);
    Assert.assertEquals("Unexpected steps in batch, after system exception", successFullyCompletedSteps, batchAfterSysException.getStepNames());
}
Also used : UserTransaction(javax.transaction.UserTransaction) StatelessEJBLocator(org.jboss.ejb.client.StatelessEJBLocator) URISyntaxException(java.net.URISyntaxException) Test(org.junit.Test)

Aggregations

StatelessEJBLocator (org.jboss.ejb.client.StatelessEJBLocator)12 Test (org.junit.Test)11 UserTransaction (javax.transaction.UserTransaction)5 AppClientWrapper (org.jboss.as.test.integration.ee.appclient.util.AppClientWrapper)3 ModelNode (org.jboss.dmr.ModelNode)3 URISyntaxException (java.net.URISyntaxException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 EJBException (javax.ejb.EJBException)1 NoSuchEJBException (javax.ejb.NoSuchEJBException)1 PortableRemoteObject (javax.rmi.PortableRemoteObject)1 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)1 EJBHomeLocator (org.jboss.ejb.client.EJBHomeLocator)1 EntityEJBLocator (org.jboss.ejb.client.EntityEJBLocator)1 StatefulEJBLocator (org.jboss.ejb.client.StatefulEJBLocator)1