2016년 8월 25일 목요일

Setting in-memory queue as camel seda

You can set the in-memory queue by using camel seda.

- Structure















- build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
    compile group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.17.0'
}

- TestRouter.java

package org.blog.test.route;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.ThreadPoolProfile;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class TestRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("seda:start?size=10").streamCaching().threads(10).process(exchange -> log.info("process message : [{}]", exchange.getIn().getBody())).end();
    }
}

- SedaApplication.java

package org.blog.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.seda.SedaEndpoint;
import org.apache.camel.spi.ThreadPoolProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@SpringBootApplication
@RestController
@Slf4j
public class SedaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SedaApplication.class, args);
    }

    @Autowired
    private ProducerTemplate producerTemplate;

    @Autowired
    private CamelContext camelContext;


    @RequestMapping("/send")
    public String sendMessage() throws Exception {
        producerTemplate.sendBody("seda:start", "fewfe");
        SedaEndpoint sedaEndpoint = (SedaEndpoint) camelContext.getEndpoint("seda:start");
        StringBuilder builder = new StringBuilder();

        for (Exchange exchange : sedaEndpoint.getExchanges()) {
            builder.append(exchange.getIn().getBody());
        }
        return builder.toString();
    }

    @PostConstruct
    private void init() {
        ThreadPoolProfile profile = new ThreadPoolProfile();
        profile.setMaxPoolSize(10);
        profile.setDefaultProfile(true);
        profile.setId("customedProfile");
        camelContext.getExecutorServiceManager().setDefaultThreadPoolProfile(profile);
    }
}

- result

You can find that seda is working well.

댓글 없음 :

댓글 쓰기