Search in sources :

Example 1 with NettyComponent

use of org.apache.camel.component.netty4.NettyComponent in project camel by apache.

the class NettyComponentAutoConfiguration method configureNettyComponent.

@Lazy
@Bean(name = "netty4-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(NettyComponent.class)
public NettyComponent configureNettyComponent(CamelContext camelContext, NettyComponentConfiguration configuration) throws Exception {
    NettyComponent component = new NettyComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null, false);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : HashMap(java.util.HashMap) NettyComponent(org.apache.camel.component.netty4.NettyComponent) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with NettyComponent

use of org.apache.camel.component.netty4.NettyComponent 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)

Aggregations

NettyComponent (org.apache.camel.component.netty4.NettyComponent)2 ByteBuf (io.netty.buffer.ByteBuf)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AsyncCallback (org.apache.camel.AsyncCallback)1 AsyncProcessor (org.apache.camel.AsyncProcessor)1 Exchange (org.apache.camel.Exchange)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DefaultManagementNameStrategy (org.apache.camel.impl.DefaultManagementNameStrategy)1 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)1 SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1