Search in sources :

Example 1 with AbstractMessageHandler

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

the class IntegrationManagementConfigurerTests method testDefaults.

@Test
public void testDefaults() {
    DirectChannel channel = new DirectChannel();
    AbstractMessageHandler handler = new RecipientListRouter();
    AbstractMessageSource<?> source = new AbstractMessageSource<Object>() {

        @Override
        public String getComponentType() {
            return null;
        }

        @Override
        protected Object doReceive() {
            return null;
        }
    };
    assertTrue(channel.isLoggingEnabled());
    assertTrue(handler.isLoggingEnabled());
    assertTrue(source.isLoggingEnabled());
    channel.setCountsEnabled(true);
    channel.setStatsEnabled(true);
    ApplicationContext ctx = mock(ApplicationContext.class);
    Map<String, IntegrationManagement> beans = new HashMap<String, IntegrationManagement>();
    beans.put("foo", channel);
    beans.put("bar", handler);
    beans.put("baz", source);
    when(ctx.getBeansOfType(IntegrationManagement.class)).thenReturn(beans);
    IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
    configurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
    configurer.setApplicationContext(ctx);
    configurer.setDefaultLoggingEnabled(false);
    configurer.afterSingletonsInstantiated();
    assertFalse(channel.isLoggingEnabled());
    assertFalse(handler.isLoggingEnabled());
    assertFalse(source.isLoggingEnabled());
    assertTrue(channel.isCountsEnabled());
    assertTrue(channel.isStatsEnabled());
}
Also used : IntegrationManagementConfigurer(org.springframework.integration.config.IntegrationManagementConfigurer) ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) EnableIntegrationManagement(org.springframework.integration.config.EnableIntegrationManagement) RecipientListRouter(org.springframework.integration.router.RecipientListRouter) AbstractMessageSource(org.springframework.integration.endpoint.AbstractMessageSource) AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) Test(org.junit.Test)

Example 2 with AbstractMessageHandler

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

the class AggregatorTests method testCustomAggPerf.

@Test
public void testCustomAggPerf() throws InterruptedException, ExecutionException, TimeoutException {
    class CustomHandler extends AbstractMessageHandler {

        // custom aggregator, only handles a single correlation
        private final ReentrantLock lock = new ReentrantLock();

        private final Collection<Message<?>> messages = new ArrayList<Message<?>>(60000);

        private final MessageChannel outputChannel;

        private CustomHandler(MessageChannel outputChannel) {
            this.outputChannel = outputChannel;
        }

        @Override
        public void handleMessageInternal(Message<?> requestMessage) {
            lock.lock();
            try {
                this.messages.add(requestMessage);
                if (this.messages.size() == 60000) {
                    List<Object> payloads = new ArrayList<Object>(this.messages.size());
                    for (Message<?> message : this.messages) {
                        payloads.add(message.getPayload());
                    }
                    this.messages.clear();
                    outputChannel.send(getMessageBuilderFactory().withPayload(payloads).copyHeaders(requestMessage.getHeaders()).build());
                }
            } finally {
                lock.unlock();
            }
        }
    }
    DirectChannel outputChannel = new DirectChannel();
    CustomHandler handler = new CustomHandler(outputChannel);
    final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>();
    outputChannel.subscribe(message -> {
        Collection<?> payload = (Collection<?>) message.getPayload();
        logger.warn("Received " + payload.size());
        resultFuture.complete(payload);
    });
    Message<?> message = new GenericMessage<String>("foo");
    StopWatch stopwatch = new StopWatch();
    stopwatch.start();
    for (int i = 0; i < 120000; i++) {
        if (i % 10000 == 0) {
            stopwatch.stop();
            logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
            stopwatch.start();
        }
        handler.handleMessage(message);
    }
    stopwatch.stop();
    logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
    Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(result);
    assertEquals(60000, result.size());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) ArrayList(java.util.ArrayList) StopWatch(org.springframework.util.StopWatch) GenericMessage(org.springframework.messaging.support.GenericMessage) CompletableFuture(java.util.concurrent.CompletableFuture) MessageChannel(org.springframework.messaging.MessageChannel) Collection(java.util.Collection) AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 AbstractMessageHandler (org.springframework.integration.handler.AbstractMessageHandler)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 EnableIntegrationManagement (org.springframework.integration.config.EnableIntegrationManagement)1 IntegrationManagementConfigurer (org.springframework.integration.config.IntegrationManagementConfigurer)1 AbstractMessageSource (org.springframework.integration.endpoint.AbstractMessageSource)1 RecipientListRouter (org.springframework.integration.router.RecipientListRouter)1 Message (org.springframework.messaging.Message)1 MessageChannel (org.springframework.messaging.MessageChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 StopWatch (org.springframework.util.StopWatch)1