Search in sources :

Example 1 with Service

use of com.adaptris.core.Service in project interlok by adaptris.

the class StoredProcedureProducerTest method testProduce_PooledConnection.

@Test
public void testProduce_PooledConnection() throws Exception {
    int maxServices = 5;
    final int iterations = 5;
    int poolsize = maxServices - 1;
    if (areTestsEnabled()) {
        List<Service> serviceList = new ArrayList<Service>();
        String name = Thread.currentThread().getName();
        Thread.currentThread().setName(getName());
        JdbcPooledConnection conn = PooledConnectionHelper.createPooledConnection(PROPERTIES.getProperty(JDBC_DRIVER), PROPERTIES.getProperty(JDBC_URL), poolsize);
        conn.setUsername(PROPERTIES.getProperty(JDBC_USER));
        conn.setPassword(PROPERTIES.getProperty(JDBC_PASSWORD));
        try {
            for (int i = 0; i < maxServices; i++) {
                JdbcStoredProcedureProducer spp = new JdbcStoredProcedureProducer();
                spp.setProcedureName(("one_in"));
                JdbcStringPayloadParameter inParameter = new JdbcStringPayloadParameter();
                inParameter.setName("xType");
                inParameter.setType(ParameterValueType.VARCHAR);
                AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Sold");
                InParameters inParameters = new InParameters();
                inParameters.add(inParameter);
                spp.setInParameters(inParameters);
                StandaloneProducer service = configureForTests(spp, false);
                service.setConnection(conn);
                serviceList.add(service);
                start(service);
            }
            PooledConnectionHelper.executeTest(serviceList, iterations, new PooledConnectionHelper.MessageCreator() {

                @Override
                public AdaptrisMessage createMsgForPooledConnectionTest() throws Exception {
                    return createMessage("Sold");
                }
            });
            assertEquals(0, conn.currentBusyConnectionCount());
            assertEquals(poolsize, conn.currentIdleConnectionCount());
            assertEquals(poolsize, conn.currentConnectionCount());
        } finally {
            stop(serviceList.toArray(new ComponentLifecycle[0]));
            Thread.currentThread().setName(name);
        }
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ArrayList(java.util.ArrayList) Service(com.adaptris.core.Service) ServiceException(com.adaptris.core.ServiceException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) ComponentLifecycle(com.adaptris.core.ComponentLifecycle) StandaloneProducer(com.adaptris.core.StandaloneProducer) Test(org.junit.Test)

Example 2 with Service

use of com.adaptris.core.Service in project interlok by adaptris.

the class PooledConnectionHelper method executeTest.

public static void executeTest(List<Service> serviceList, final int iterations, final MessageCreator creator) throws Exception {
    MyExceptionHandler handler = new MyExceptionHandler();
    List<Thread> threads = new ArrayList<>();
    for (final Service s : serviceList) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int j = 0; j < iterations; j++) {
                        final AdaptrisMessage msg = creator.createMsgForPooledConnectionTest();
                        s.doService(msg);
                        TimeUnit.MILLISECONDS.sleep(DEFAULT_PAUSE.toMilliseconds());
                    }
                } catch (Exception e) {
                    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                }
            }
        });
        t.setUncaughtExceptionHandler(handler);
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        if (t.isAlive())
            t.join();
    }
    if (handler.hasError()) {
        throw handler.lastCaughtException();
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ArrayList(java.util.ArrayList) Service(com.adaptris.core.Service)

Example 3 with Service

use of com.adaptris.core.Service in project interlok by adaptris.

the class ActiveMqJmsTransactedWorkflowTest method testServiceExceptionNonStrictWithErrorHandler.

// In Non-Strict Mode, if you have configured an error handler, then
// the transaction is successful.
@Test
public void testServiceExceptionNonStrictWithErrorHandler() throws Exception {
    int msgCount = 10;
    String destination = createSafeUniqueId(new Object());
    MockMessageProducer meh = new MockMessageProducer();
    Channel channel = createStartableChannel(activeMqBroker, true, "testServiceExceptionNonStrictWithErrorHandler", destination);
    JmsTransactedWorkflow workflow = (JmsTransactedWorkflow) channel.getWorkflowList().get(0);
    workflow.setStrict(Boolean.FALSE);
    workflow.getServiceCollection().addService(new ThrowExceptionService(new ConfiguredException("Fail")));
    channel.setMessageErrorHandler(new StandardProcessingExceptionHandler(new ServiceList(new ArrayList<Service>(Arrays.asList(new Service[] { new StandaloneProducer(meh) })))));
    channel.prepare();
    try {
        channel.requestStart();
        StandaloneProducer sender = new StandaloneProducer(activeMqBroker.getJmsConnection(), new PtpProducer().withQueue((destination)));
        send(sender, msgCount);
        waitForMessages(meh, msgCount);
        assertEquals(0, ((MockMessageProducer) workflow.getProducer()).getMessages().size());
    } finally {
        channel.requestClose();
    }
    assertEquals(0, activeMqBroker.messagesOnQueue(destination));
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) ServiceList(com.adaptris.core.ServiceList) MockChannel(com.adaptris.core.stubs.MockChannel) Channel(com.adaptris.core.Channel) JmsTransactedWorkflow(com.adaptris.core.jms.JmsTransactedWorkflow) ConfiguredException(com.adaptris.core.services.exception.ConfiguredException) Service(com.adaptris.core.Service) MockSkipProducerService(com.adaptris.core.stubs.MockSkipProducerService) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) PtpProducer(com.adaptris.core.jms.PtpProducer) StandardProcessingExceptionHandler(com.adaptris.core.StandardProcessingExceptionHandler) StandaloneProducer(com.adaptris.core.StandaloneProducer) Test(org.junit.Test)

Example 4 with Service

use of com.adaptris.core.Service in project interlok by adaptris.

the class ActiveMqJmsTransactedWorkflowTest method testWorkflow_SkipProducer.

@Test
public void testWorkflow_SkipProducer() throws Exception {
    int msgCount = 10;
    String destination = createSafeUniqueId(new Object());
    Channel channel = createStartableChannel(activeMqBroker, true, "testWorkflow_SkipProducer", destination);
    JmsTransactedWorkflow workflow = (JmsTransactedWorkflow) channel.getWorkflowList().get(0);
    MockMessageProducer serviceProducer = new MockMessageProducer();
    workflow.getServiceCollection().addAll(Arrays.asList(new Service[] { new StandaloneProducer(serviceProducer), new MockSkipProducerService() }));
    MockMessageProducer workflowProducer = (MockMessageProducer) workflow.getProducer();
    try {
        channel.requestStart();
        StandaloneProducer sender = new StandaloneProducer(activeMqBroker.getJmsConnection(), new PtpProducer().withQueue((destination)));
        send(sender, msgCount);
        waitForMessages(serviceProducer, msgCount);
        assertEquals(msgCount, serviceProducer.messageCount());
        assertEquals(0, workflowProducer.messageCount());
    } finally {
        channel.requestClose();
    }
    assertEquals(0, activeMqBroker.messagesOnQueue(destination));
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) MockSkipProducerService(com.adaptris.core.stubs.MockSkipProducerService) MockChannel(com.adaptris.core.stubs.MockChannel) Channel(com.adaptris.core.Channel) JmsTransactedWorkflow(com.adaptris.core.jms.JmsTransactedWorkflow) Service(com.adaptris.core.Service) MockSkipProducerService(com.adaptris.core.stubs.MockSkipProducerService) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) PtpProducer(com.adaptris.core.jms.PtpProducer) StandaloneProducer(com.adaptris.core.StandaloneProducer) Test(org.junit.Test)

Example 5 with Service

use of com.adaptris.core.Service in project interlok by adaptris.

the class LargeMessageWorkflowTest method testServiceException.

@Override
@Test
public void testServiceException() throws Exception {
    MockMessageProducer producer = new MockMessageProducer();
    MockMessageProducer meh = new MockMessageProducer();
    MockChannel channel = createChannel(producer, Arrays.asList(new Service[] { new AddMetadataService(Arrays.asList(new MetadataElement[] { new MetadataElement(METADATA_KEY, METADATA_VALUE) })), new PayloadFromTemplateService().withTemplate(PAYLOAD_2), new ThrowExceptionService(new ConfiguredException("Fail")) }));
    try {
        LargeMessageWorkflow workflow = (LargeMessageWorkflow) channel.getWorkflowList().get(0);
        channel.setMessageErrorHandler(new StandardProcessingExceptionHandler(new ServiceList(new ArrayList<Service>(Arrays.asList(new Service[] { new StandaloneProducer(meh) })))));
        channel.prepare();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(PAYLOAD_1);
        start(channel);
        workflow.onAdaptrisMessage(msg);
        assertEquals("Make none produced", 0, producer.getMessages().size());
        assertEquals(1, meh.getMessages().size());
        for (AdaptrisMessage m : meh.getMessages()) {
            assertEquals(PAYLOAD_2, m.getContent());
            assertTrue("Contains correct metadata key", m.containsKey(METADATA_KEY));
            assertEquals(METADATA_VALUE, m.getMetadataValue(METADATA_KEY));
            assertNotNull(m.getObjectHeaders().get(CoreConstants.OBJ_METADATA_EXCEPTION));
            assertNotNull(m.getObjectHeaders().get(CoreConstants.OBJ_METADATA_EXCEPTION_CAUSE));
            assertEquals(ThrowExceptionService.class.getSimpleName(), m.getObjectHeaders().get(CoreConstants.OBJ_METADATA_EXCEPTION_CAUSE));
        }
    } finally {
        stop(channel);
    }
}
Also used : MockChannel(com.adaptris.core.stubs.MockChannel) MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) ServiceList(com.adaptris.core.ServiceList) ConfiguredException(com.adaptris.core.services.exception.ConfiguredException) PayloadFromTemplateService(com.adaptris.core.services.metadata.PayloadFromTemplateService) Service(com.adaptris.core.Service) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) MetadataElement(com.adaptris.core.MetadataElement) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) StandardProcessingExceptionHandler(com.adaptris.core.StandardProcessingExceptionHandler) PayloadFromTemplateService(com.adaptris.core.services.metadata.PayloadFromTemplateService) StandaloneProducer(com.adaptris.core.StandaloneProducer) Test(org.junit.Test) StandardWorkflowTest(com.adaptris.core.StandardWorkflowTest)

Aggregations

Service (com.adaptris.core.Service)49 Test (org.junit.Test)38 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)28 ServiceList (com.adaptris.core.ServiceList)16 ArrayList (java.util.ArrayList)16 ComponentLifecycle (com.adaptris.core.ComponentLifecycle)14 AdvancedJdbcPooledConnection (com.adaptris.core.jdbc.AdvancedJdbcPooledConnection)12 PooledConnectionHelper (com.adaptris.core.jdbc.PooledConnectionHelper)12 ServiceException (com.adaptris.core.ServiceException)11 MetadataElement (com.adaptris.core.MetadataElement)10 NullService (com.adaptris.core.NullService)10 StandaloneProducer (com.adaptris.core.StandaloneProducer)10 AddMetadataService (com.adaptris.core.services.metadata.AddMetadataService)10 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)9 LogMessageService (com.adaptris.core.services.LogMessageService)8 ThrowExceptionService (com.adaptris.core.services.exception.ThrowExceptionService)7 MockChannel (com.adaptris.core.stubs.MockChannel)7 StandardProcessingExceptionHandler (com.adaptris.core.StandardProcessingExceptionHandler)6 JdbcPooledConnection (com.adaptris.core.jdbc.JdbcPooledConnection)6 GuidGenerator (com.adaptris.util.GuidGenerator)6