Search in sources :

Example 31 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project opennms by OpenNMS.

the class SyslogReceiverCamelNettyImpl method run.

/**
 * The execution context.
 */
@Override
public void run() {
    // Setup logging and create the dispatcher
    super.run();
    SimpleRegistry registry = new SimpleRegistry();
    // Adding netty component to camel in order to resolve OSGi loading issues
    NettyComponent nettyComponent = new NettyComponent();
    m_camel = new DefaultCamelContext(registry);
    // Set the context name so that it shows up nicely in JMX
    // 
    // @see org.apache.camel.management.DefaultManagementNamingStrategy
    // 
    // m_camel.setManagementName("org.opennms.features.events.syslog.listener");
    m_camel.setName("syslogdListenerCamelNettyContext");
    m_camel.setManagementNameStrategy(new DefaultManagementNameStrategy(m_camel, "#name#", null));
    m_camel.addComponent("netty4", nettyComponent);
    m_camel.getShutdownStrategy().setShutdownNowOnTimeout(true);
    m_camel.getShutdownStrategy().setTimeout(15);
    m_camel.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
    try {
        m_camel.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                String from = String.format("netty4:udp://%s:%d?sync=false&allowDefaultCodec=false&receiveBufferSize=%d&connectTimeout=%d", InetAddressUtils.str(m_host), m_port, Integer.MAX_VALUE, SOCKET_TIMEOUT);
                from(from).routeId("syslogListen").process(new AsyncProcessor() {

                    @Override
                    public void process(Exchange exchange) throws Exception {
                        final ByteBuf buffer = exchange.getIn().getBody(ByteBuf.class);
                        // NettyConstants.NETTY_REMOTE_ADDRESS is a SocketAddress type but because
                        // we are listening on an InetAddress, it will always be of type InetAddressSocket
                        InetSocketAddress source = (InetSocketAddress) exchange.getIn().getHeader(NettyConstants.NETTY_REMOTE_ADDRESS);
                        // Synchronously invoke the dispatcher
                        m_dispatcher.send(new SyslogConnection(source, buffer.nioBuffer())).get();
                    }

                    @Override
                    public boolean process(Exchange exchange, AsyncCallback callback) {
                        final ByteBuf buffer = exchange.getIn().getBody(ByteBuf.class);
                        // NettyConstants.NETTY_REMOTE_ADDRESS is a SocketAddress type but because
                        // we are listening on an InetAddress, it will always be of type InetAddressSocket
                        InetSocketAddress source = (InetSocketAddress) exchange.getIn().getHeader(NettyConstants.NETTY_REMOTE_ADDRESS);
                        ByteBuffer bufferCopy = ByteBuffer.allocate(buffer.readableBytes());
                        buffer.getBytes(buffer.readerIndex(), bufferCopy);
                        m_dispatcher.send(new SyslogConnection(source, bufferCopy)).whenComplete((r, e) -> {
                            if (e != null) {
                                exchange.setException(e);
                            }
                            callback.done(false);
                        });
                        return false;
                    }
                });
            }
        });
        m_camel.start();
    } catch (Throwable e) {
        LOG.error("Could not configure Camel routes for syslog receiver", e);
    }
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) InetSocketAddress(java.net.InetSocketAddress) NettyComponent(org.apache.camel.component.netty4.NettyComponent) AsyncCallback(org.apache.camel.AsyncCallback) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Exchange(org.apache.camel.Exchange) DefaultManagementNameStrategy(org.apache.camel.impl.DefaultManagementNameStrategy) AsyncProcessor(org.apache.camel.AsyncProcessor) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection)

Example 32 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project quickstarts by jboss-switchyard.

the class HL7Client method testSendA19.

public void testSendA19() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    HL7MLLPCodec codec = new HL7MLLPCodec();
    codec.setCharset("iso-8859-1");
    codec.setConvertLFtoCR(true);
    registry.put("hl7codec", codec);
    CamelContext camelContext = new DefaultCamelContext(registry);
    camelContext.start();
    ProducerTemplate template = camelContext.createProducerTemplate();
    String line1 = "MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4";
    String line2 = "QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||";
    StringBuilder in = new StringBuilder();
    in.append(line1);
    in.append("\r");
    in.append(line2);
    template.requestBody("mina2:tcp://127.0.0.1:" + MINA2_PORT + "?sync=true&codec=#hl7codec", in.toString());
    template.stop();
    camelContext.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) HL7MLLPCodec(org.apache.camel.component.hl7.HL7MLLPCodec) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 33 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project camel by apache.

the class XmlRpcEndpointTest method createRegistry.

protected Registry createRegistry() {
    SimpleRegistry answer = new SimpleRegistry();
    // Binding the client configurer
    answer.put("myClientConfigurer", new MyClientConfigurer());
    return answer;
}
Also used : SimpleRegistry(org.apache.camel.impl.SimpleRegistry)

Example 34 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project camel by apache.

the class HystrixHierarchicalConfigTest method testRegistryConfiguration.

@Test
public void testRegistryConfiguration() throws Exception {
    final SimpleRegistry registry = new SimpleRegistry();
    final CamelContext context = new DefaultCamelContext(registry);
    HystrixConfigurationDefinition def = new HystrixConfigurationDefinition();
    def.setGroupKey("global-group-key");
    def.setThreadPoolKey("global-thread-key");
    def.setCorePoolSize(10);
    HystrixConfigurationDefinition ref = new HystrixConfigurationDefinition();
    ref.setGroupKey("ref-group-key");
    ref.setCorePoolSize(5);
    registry.put(HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID, def);
    registry.put("ref-hystrix", ref);
    final HystrixProcessorFactory factory = new HystrixProcessorFactory();
    final HystrixConfigurationDefinition config = factory.buildHystrixConfiguration(context, new HystrixDefinition().hystrixConfiguration("ref-hystrix").hystrixConfiguration().groupKey("local-conf-group-key").requestLogEnabled(false).end());
    Assert.assertEquals("local-conf-group-key", config.getGroupKey());
    Assert.assertEquals("global-thread-key", config.getThreadPoolKey());
    Assert.assertEquals(new Integer(5), config.getCorePoolSize());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) HystrixDefinition(org.apache.camel.model.HystrixDefinition) Test(org.junit.Test)

Example 35 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project camel by apache.

the class HystrixHierarchicalConfigTest method testMixedConfiguration.

@Test
public void testMixedConfiguration() throws Exception {
    final SimpleRegistry registry = new SimpleRegistry();
    final CamelContext context = new DefaultCamelContext(registry);
    HystrixConfigurationDefinition def = new HystrixConfigurationDefinition();
    def.setGroupKey("global-group-key");
    def.setThreadPoolKey("global-thread-key");
    def.setCorePoolSize(10);
    HystrixConfigurationDefinition ref = new HystrixConfigurationDefinition();
    ref.setGroupKey("ref-group-key");
    ref.setCorePoolSize(5);
    // this should be ignored
    HystrixConfigurationDefinition defReg = new HystrixConfigurationDefinition();
    defReg.setGroupKey("global-reg-group-key");
    defReg.setThreadPoolKey("global-reg-thread-key");
    defReg.setCorePoolSize(20);
    context.setHystrixConfiguration(def);
    registry.put(HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID, defReg);
    registry.put("ref-hystrix", ref);
    final HystrixProcessorFactory factory = new HystrixProcessorFactory();
    final HystrixConfigurationDefinition config = factory.buildHystrixConfiguration(context, new HystrixDefinition().hystrixConfiguration("ref-hystrix").hystrixConfiguration().groupKey("local-conf-group-key").requestLogEnabled(false).end());
    Assert.assertEquals("local-conf-group-key", config.getGroupKey());
    Assert.assertEquals("global-thread-key", config.getThreadPoolKey());
    Assert.assertEquals(new Integer(5), config.getCorePoolSize());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) HystrixDefinition(org.apache.camel.model.HystrixDefinition) Test(org.junit.Test)

Aggregations

SimpleRegistry (org.apache.camel.impl.SimpleRegistry)60 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)57 CamelContext (org.apache.camel.CamelContext)35 Test (org.junit.Test)25 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 Before (org.junit.Before)8 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 Component (org.apache.camel.Component)4 Exchange (org.apache.camel.Exchange)4 DummyRestConsumerFactory (org.apache.camel.component.rest.DummyRestConsumerFactory)4 HashMap (java.util.HashMap)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 SjmsComponent (org.apache.camel.component.sjms.SjmsComponent)3 Config (com.hazelcast.config.Config)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 Properties (java.util.Properties)2 ConnectionFactory (javax.jms.ConnectionFactory)2 FileComponent (org.apache.camel.component.file.FileComponent)2 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)2