Search in sources :

Example 21 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testErrorFlow.

@Test
public void testErrorFlow() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    scf.setSingleUse(true);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    SubscribableChannel errorChannel = new DirectChannel();
    final String errorMessage = "An error occurred";
    errorChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(new GenericMessage<String>(errorMessage));
    });
    gateway.setErrorChannel(errorChannel);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final SubscribableChannel channel = new DirectChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    channel.subscribe(handler);
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    byte[] bytes = new byte[errorMessage.length() + 2];
    readFully(socket1.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
    readFully(socket2.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessageChannel(org.springframework.messaging.MessageChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 22 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpReceivingChannelAdapterTests method testException.

@Test
public void testException() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    SubscribableChannel channel = new DirectChannel();
    adapter.setOutputChannel(channel);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    channel.subscribe(handler);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = errorChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
    message = errorChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) IOException(java.io.IOException) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 23 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class GroovyControlBusFactoryBean method createHandler.

@Override
protected MessageHandler createHandler() {
    Binding binding = new ManagedBeansBinding(this.getBeanFactory());
    GroovyCommandMessageProcessor processor = new GroovyCommandMessageProcessor(binding, message -> {
        Map<String, Object> variables = new HashMap<>();
        variables.put("headers", message.getHeaders());
        return variables;
    });
    if (this.customizer != null) {
        processor.setCustomizer(this.customizer);
    }
    if (this.beanClassLoader != null) {
        processor.setBeanClassLoader(this.beanClassLoader);
    }
    if (getBeanFactory() != null) {
        processor.setBeanFactory(getBeanFactory());
    }
    return this.configureHandler(new ServiceActivatingHandler(processor));
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) GroovyCommandMessageProcessor(org.springframework.integration.groovy.GroovyCommandMessageProcessor) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler)

Example 24 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class DirectChannelSubscriptionTests method sendAndReceiveForRegisteredEndpoint.

@Test
public void sendAndReceiveForRegisteredEndpoint() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "handle");
    serviceActivator.setOutputChannel(targetChannel);
    EventDrivenConsumer endpoint = new EventDrivenConsumer(sourceChannel, serviceActivator);
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    this.sourceChannel.send(new GenericMessage<String>("foo"));
    Message<?> response = this.targetChannel.receive();
    assertEquals("foo!", response.getPayload());
    context.stop();
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 25 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class ServiceActivatorEndpointTests method noReplyTarget.

@Test(expected = MessagingException.class)
public void noReplyTarget() {
    ServiceActivatingHandler endpoint = this.createEndpoint();
    Message<?> message = MessageBuilder.withPayload("foo").build();
    endpoint.handleMessage(message);
}
Also used : ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Aggregations

ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)45 Test (org.junit.Test)38 QueueChannel (org.springframework.integration.channel.QueueChannel)29 BeanFactory (org.springframework.beans.factory.BeanFactory)10 DirectChannel (org.springframework.integration.channel.DirectChannel)10 GenericMessage (org.springframework.messaging.support.GenericMessage)10 Message (org.springframework.messaging.Message)8 ServerSocket (java.net.ServerSocket)7 Socket (java.net.Socket)7 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)6 AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)6 RequestReplyExchanger (org.springframework.integration.gateway.RequestReplyExchanger)5 Date (java.util.Date)4 TcpNetServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 PublishSubscribeChannel (org.springframework.integration.channel.PublishSubscribeChannel)3 TestUtils (org.springframework.integration.test.util.TestUtils)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 HashMap (java.util.HashMap)2