Search in sources :

Example 61 with SimpleRegistry

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

the class OsgiComponentResolverTest method testOsgiResolverFindComponentFallbackTest.

@Test
public void testOsgiResolverFindComponentFallbackTest() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("allstar-component", new SampleComponent(true));
    CamelContext camelContext = new DefaultCamelContext(registry);
    OsgiComponentResolver resolver = new OsgiComponentResolver(getBundleContext());
    Component component = resolver.resolveComponent("allstar", camelContext);
    assertNotNull("We should find the super component", component);
    assertTrue("We should get the super component here", component instanceof SampleComponent);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) Component(org.apache.camel.Component) FileComponent(org.apache.camel.component.file.FileComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 62 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 63 with SimpleRegistry

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

the class EchoRpcIT method throwsRequestTimedOutExceptionOnTimeout.

@Test(timeout = CamelRpcClientPreProcessor.CAMEL_JMS_REQUEST_TIMEOUT_DEFAULT * 4)
public void throwsRequestTimedOutExceptionOnTimeout() throws Exception {
    assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation());
    EchoRpcModule echoRpcModule = new EchoRpcModule();
    SimpleRegistry registry = new SimpleRegistry();
    CamelContext context = new DefaultCamelContext(registry);
    context.getShutdownStrategy().setTimeout(5);
    context.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
    context.addComponent("queuingservice", queuingservice);
    CamelRpcServerRouteManager routeManager = new CamelRpcServerRouteManager(context, new MockMinionIdentity(REMOTE_LOCATION_NAME));
    routeManager.bind(echoRpcModule);
    EchoRequest request = new EchoRequest("HELLO!!!");
    request.setLocation(REMOTE_LOCATION_NAME);
    request.setDelay(CamelRpcClientPreProcessor.CAMEL_JMS_REQUEST_TIMEOUT_DEFAULT * 2);
    try {
        echoClient.execute(request).get();
        fail("Did not get ExecutionException");
    } catch (ExecutionException e) {
        assertTrue("Cause is not of type RequestTimedOutException: " + ExceptionUtils.getStackTrace(e), e.getCause() instanceof RequestTimedOutException);
        // Verify that the message body was suppressed
        MockLogAppender.assertLogMatched(Level.DEBUG, "[Body is not logged]");
    }
    routeManager.unbind(echoRpcModule);
    context.stop();
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) EchoRpcModule(org.opennms.core.rpc.echo.EchoRpcModule) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) ExecutionException(java.util.concurrent.ExecutionException) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 64 with SimpleRegistry

use of org.apache.camel.impl.SimpleRegistry in project chuidiang-ejemplos by chuidiang.

the class RouteBinMain method startCamel.

private static CamelContext startCamel() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("FromDecoder", new MessageDecoder());
    registry.put("FromEncoder", new MessageEncoder());
    registry.put("ToDecoder", new MessageDecoder());
    registry.put("ToEncoder", new MessageEncoder());
    final CamelContext context = new DefaultCamelContext(registry);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            createNettyRoute();
        }

        protected void createNettyRoute() {
            from("netty:tcp://localhost:55559?encoder=#FromEncoder&" + "decoder=#FromDecoder&" + "disconnectOnNoReply=false&" + "serverClosedChannelExceptionCaughtLogLevel=WARN&" + "keepAlive=true").to("netty:tcp://localhost:55560?encoder=#ToEncoder&" + "decoder=#ToDecoder&" + "disconnectOnNoReply=false&" + "serverClosedChannelExceptionCaughtLogLevel=WARN&" + "keepAlive=true").setId("myRoute");
        }
    });
    context.start();
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

SimpleRegistry (org.apache.camel.impl.SimpleRegistry)64 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)61 CamelContext (org.apache.camel.CamelContext)40 Test (org.junit.Test)31 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 Before (org.junit.Before)8 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)5 EchoRequest (org.opennms.core.rpc.echo.EchoRequest)5 EchoRpcModule (org.opennms.core.rpc.echo.EchoRpcModule)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 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 EchoResponse (org.opennms.core.rpc.echo.EchoResponse)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