Search in sources :

Example 1 with SyncTaskExecutor

use of org.springframework.core.task.SyncTaskExecutor in project spring-integration by spring-projects.

the class ExecutorChannelTests method interceptorWithException.

@Test
public void interceptorWithException() {
    ExecutorChannel channel = new ExecutorChannel(new SyncTaskExecutor());
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    Message<Object> message = new GenericMessage<Object>("foo");
    MessageHandler handler = mock(MessageHandler.class);
    IllegalStateException expected = new IllegalStateException("Fake exception");
    willThrow(expected).given(handler).handleMessage(message);
    BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
    channel.addInterceptor(interceptor);
    channel.subscribe(handler);
    try {
        channel.send(message);
    } catch (MessageDeliveryException actual) {
        assertSame(expected, actual.getCause());
    }
    verify(handler).handleMessage(message);
    assertEquals(1, interceptor.getCounter().get());
    assertTrue(interceptor.wasAfterHandledInvoked());
}
Also used : SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 2 with SyncTaskExecutor

use of org.springframework.core.task.SyncTaskExecutor in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiatorTests method testGracefulLeaderSelectorExit.

@Test
public void testGracefulLeaderSelectorExit() throws Exception {
    AtomicReference<Throwable> throwableAtomicReference = new AtomicReference<>();
    LockRegistry registry = mock(LockRegistry.class);
    Lock lock = spy(new ReentrantLock());
    willAnswer(invocation -> {
        try {
            return invocation.callRealMethod();
        } catch (Throwable e) {
            throwableAtomicReference.set(e);
            throw e;
        }
    }).given(lock).unlock();
    given(registry.obtain(anyString())).willReturn(lock);
    LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator(registry);
    willAnswer(invocation -> {
        another.stop();
        return false;
    }).given(lock).tryLock(anyLong(), eq(TimeUnit.MILLISECONDS));
    new DirectFieldAccessor(another).setPropertyValue("executorService", new ExecutorServiceAdapter(new SyncTaskExecutor()));
    another.start();
    Throwable throwable = throwableAtomicReference.get();
    assertNull(throwable);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DefaultLockRegistry(org.springframework.integration.support.locks.DefaultLockRegistry) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutorServiceAdapter(org.springframework.core.task.support.ExecutorServiceAdapter) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 3 with SyncTaskExecutor

use of org.springframework.core.task.SyncTaskExecutor in project cloudbreak by hortonworks.

the class AbstractFlowConfiguration method getStateMachineConfiguration.

protected MachineConfiguration<S, E> getStateMachineConfiguration() {
    StateMachineConfigurationBuilder<S, E> configurationBuilder = new StateMachineConfigurationBuilder<>(ObjectPostProcessor.QUIESCENT_POSTPROCESSOR, true);
    StateMachineStateBuilder<S, E> stateBuilder = new StateMachineStateBuilder<>(ObjectPostProcessor.QUIESCENT_POSTPROCESSOR, true);
    StateMachineTransitionBuilder<S, E> transitionBuilder = new StateMachineTransitionBuilder<>(ObjectPostProcessor.QUIESCENT_POSTPROCESSOR, true);
    StateMachineListener<S, E> listener = new StateMachineListenerAdapter<S, E>() {

        @Override
        public void stateChanged(State<S, E> from, State<S, E> to) {
            LOGGER.debug("state changed from {} to {}", from, to);
        }

        @Override
        public void eventNotAccepted(Message<E> event) {
            LOGGER.error("{} not accepted event: {}", getClass().getSimpleName(), event);
        }
    };
    return new MachineConfiguration<>(configurationBuilder, stateBuilder, transitionBuilder, listener, new SyncTaskExecutor());
}
Also used : Message(org.springframework.messaging.Message) StateMachineStateBuilder(org.springframework.statemachine.config.builders.StateMachineStateBuilder) SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) StateMachineConfigurationBuilder(org.springframework.statemachine.config.builders.StateMachineConfigurationBuilder) StateMachineTransitionBuilder(org.springframework.statemachine.config.builders.StateMachineTransitionBuilder) FlowState(com.sequenceiq.cloudbreak.core.flow2.FlowState) State(org.springframework.statemachine.state.State) StateMachineListenerAdapter(org.springframework.statemachine.listener.StateMachineListenerAdapter)

Example 4 with SyncTaskExecutor

use of org.springframework.core.task.SyncTaskExecutor in project spring-framework by spring-projects.

the class ReactiveTypeHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
    factoryBean.afterPropertiesSet();
    ContentNegotiationManager manager = factoryBean.getObject();
    ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
    this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager);
    resetRequest();
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with SyncTaskExecutor

use of org.springframework.core.task.SyncTaskExecutor in project spring-framework by spring-projects.

the class RestTemplateXhrTransportTests method connect.

private ListenableFuture<WebSocketSession> connect(RestOperations restTemplate, ClientHttpResponse... responses) throws Exception {
    RestTemplateXhrTransport transport = new RestTemplateXhrTransport(restTemplate);
    transport.setTaskExecutor(new SyncTaskExecutor());
    SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("https://example.com"));
    HttpHeaders headers = new HttpHeaders();
    headers.add("h-foo", "h-bar");
    TransportRequest request = new DefaultTransportRequest(urlInfo, headers, headers, transport, TransportType.XHR, CODEC);
    return transport.connect(request, this.webSocketHandler);
}
Also used : SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI)

Aggregations

SyncTaskExecutor (org.springframework.core.task.SyncTaskExecutor)8 Test (org.junit.Test)3 BeanFactory (org.springframework.beans.factory.BeanFactory)2 MessageHandler (org.springframework.messaging.MessageHandler)2 StateMachineConfigurationBuilder (org.springframework.statemachine.config.builders.StateMachineConfigurationBuilder)2 StateMachineStateBuilder (org.springframework.statemachine.config.builders.StateMachineStateBuilder)2 StateMachineTransitionBuilder (org.springframework.statemachine.config.builders.StateMachineTransitionBuilder)2 FlowState (com.sequenceiq.cloudbreak.core.flow2.FlowState)1 URI (java.net.URI)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Before (org.junit.Before)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)1 AsyncTaskExecutor (org.springframework.core.task.AsyncTaskExecutor)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1 ExecutorServiceAdapter (org.springframework.core.task.support.ExecutorServiceAdapter)1 HttpHeaders (org.springframework.http.HttpHeaders)1