Example usage for javax.resource.spi.work Work Work

List of usage examples for javax.resource.spi.work Work Work

Introduction

In this page you can find the example usage for javax.resource.spi.work Work Work.

Prototype

Work

Source Link

Usage

From source file:org.tomitribe.chatterbox.slack.adapter.SlackResourceAdapter.java

public void endpointActivation(final MessageEndpointFactory messageEndpointFactory,
        final ActivationSpec activationSpec) throws ResourceException {
    final SlackActivationSpec slackActivationSpec = (SlackActivationSpec) activationSpec;

    workManager.scheduleWork(new Work() {

        @Override/*from   www. ja v a2 s .com*/
        public void run() {
            try {
                final MessageEndpoint messageEndpoint = messageEndpointFactory.createEndpoint(null);

                final EndpointTarget target = new EndpointTarget(messageEndpoint);
                final Class<?> endpointClass = slackActivationSpec.getBeanClass() != null
                        ? slackActivationSpec.getBeanClass()
                        : messageEndpointFactory.getEndpointClass();

                target.commands.addAll(Commands.get(endpointClass, target, null).values());

                for (Cmd cmd : target.commands) {
                    main.add(cmd);
                }

                targets.put(slackActivationSpec, target);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void release() {
        }

    });

}

From source file:org.mule.execution.FlowProcessingPhase.java

@Override
public void runPhase(final FlowProcessingPhaseTemplate flowProcessingPhaseTemplate,
        final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    Work flowExecutionWork = new Work() {
        @Override//from  w  w  w.  j  a v  a2s. c  o  m
        public void release() {
        }

        @Override
        public void run() {
            try {
                try {
                    TransactionalErrorHandlingExecutionTemplate transactionTemplate = TransactionalErrorHandlingExecutionTemplate
                            .createMainExecutionTemplate(
                                    messageProcessContext.getFlowConstruct().getMuleContext(),
                                    (messageProcessContext.getTransactionConfig() == null
                                            ? new MuleTransactionConfig()
                                            : messageProcessContext.getTransactionConfig()),
                                    messageProcessContext.getFlowConstruct().getExceptionListener());
                    MuleEvent response = transactionTemplate.execute(new ExecutionCallback<MuleEvent>() {
                        @Override
                        public MuleEvent process() throws Exception {
                            Object message = flowProcessingPhaseTemplate.getOriginalMessage();
                            if (message == null) {
                                return null;
                            }
                            MuleEvent muleEvent = flowProcessingPhaseTemplate.getMuleEvent();
                            muleEvent = flowProcessingPhaseTemplate.beforeRouteEvent(muleEvent);
                            muleEvent = flowProcessingPhaseTemplate.routeEvent(muleEvent);
                            muleEvent = flowProcessingPhaseTemplate.afterRouteEvent(muleEvent);
                            return muleEvent;
                        }
                    });
                    if (flowProcessingPhaseTemplate instanceof RequestResponseFlowProcessingPhaseTemplate) {
                        ((RequestResponseFlowProcessingPhaseTemplate) flowProcessingPhaseTemplate)
                                .sendResponseToClient(response);
                    }
                    flowProcessingPhaseTemplate.afterSuccessfulProcessingFlow(response);
                } catch (MessagingException e) {
                    flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
                }
                phaseResultNotifier.phaseSuccessfully();
            } catch (Exception e) {
                MuleException me = new DefaultMuleException(e);
                try {
                    flowProcessingPhaseTemplate.afterFailureProcessingFlow(me);
                } catch (MuleException e1) {
                    logger.warn("Failure during exception processing in flow template: " + e.getMessage());
                    if (logger.isDebugEnabled()) {
                        logger.debug(e);
                    }
                }
                phaseResultNotifier.phaseFailure(e);
            }
        }
    };
    if (messageProcessContext.supportsAsynchronousProcessing()) {
        try {
            messageProcessContext.getFlowExecutionWorkManager().scheduleWork(flowExecutionWork);
        } catch (WorkException e) {
            phaseResultNotifier.phaseFailure(e);
        }
    } else {
        flowExecutionWork.run();
    }
}

From source file:org.mule.MuleWorkManagerTestCase.java

@Test
public void testDoWorkExecutesSynchronously() throws Exception {
    final Thread callerThread = Thread.currentThread();

    MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
    wm.setMuleContext(muleContext);//from  w ww.  j  ava  2 s.  c  o  m

    try {
        wm.start();

        wm.doWork(new Work() {
            public void release() {
                // no-op
            }

            public void run() {
                Thread calleeThread = Thread.currentThread();
                assertEquals("WorkManager.doWork() should have been executed in the same thread.", callerThread,
                        calleeThread);
                if (logger.isDebugEnabled()) {
                    logger.debug("WORK: " + Thread.currentThread());
                }
            }
        });
        if (logger.isDebugEnabled()) {
            logger.debug("MAIN: " + Thread.currentThread());
        }
    } finally {
        wm.dispose();
    }

}

From source file:org.mule.MuleWorkManagerTestCase.java

@Test
public void testScheduleWorkExecutesAsynchronously() throws Exception {
    final Thread callerThread = Thread.currentThread();

    MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
    wm.setMuleContext(muleContext);//from  w  ww.j  a  v  a  2  s . c o m

    try {
        wm.start();

        wm.scheduleWork(new Work() {
            public void release() {
                // no-op
            }

            public void run() {
                Thread calleeThread = Thread.currentThread();
                assertFalse("WorkManager.scheduleWork() should have been executed in a different thread.",
                        callerThread.equals(calleeThread));
                if (logger.isDebugEnabled()) {
                    logger.debug("WORK: " + Thread.currentThread());
                }
            }
        });
        if (logger.isDebugEnabled()) {
            logger.debug("MAIN: " + Thread.currentThread());
        }
    } finally {
        wm.dispose();
    }

}

From source file:org.mule.MuleWorkManagerTestCase.java

@Test
public void testStartWorkExecutesAsynchronously() throws Exception {
    final Thread callerThread = Thread.currentThread();

    MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
    wm.setMuleContext(muleContext);/*  w ww . j  a v a  2 s. com*/

    try {
        wm.start();

        wm.startWork(new Work() {
            public void release() {
                // no-op
            }

            public void run() {
                Thread calleeThread = Thread.currentThread();
                assertFalse("WorkManager.startWork() should have been executed in a different thread.",
                        callerThread.equals(calleeThread));
                if (logger.isDebugEnabled()) {
                    logger.debug("WORK: " + Thread.currentThread());
                }
            }
        });
        if (logger.isDebugEnabled()) {
            logger.debug("MAIN: " + Thread.currentThread());
        }
    } finally {
        wm.dispose();
    }

}