Camel netty 组件示例在 Spring MVC 中不起作用

Camel netty component example doesn't work, within Spring MVC

我尝试了本主题中的一个漂亮示例 (http://shengwangi.blogspot.hu/2015/01/camel-netty-hello-world-simple-example.html),但它对我不起作用。 我想创建一个简单可行的 TCP 适配器,它使用自己的编码器和解码器。起初我以这种方式检查上面的例子(它使用一个简单的字符串编码器和解码器):

CamelContext ccontext = new DefaultCamelContext();

        try {
            ccontext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("netty:tcp://localhost:7000?sync=true&keepAlive=true&allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder").to("bean:sayHello");
                }
            });

            ccontext.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

并且有两个bean,按照上面的例子:

<!-- ChannelBuffer/ByteBuf to String -->
       <bean id="stringDecoder" class="org.jboss.netty.handler.codec.string.StringDecoder"/>

       <!-- String to ChannelBuffer/ByteBuf -->
       <bean id="stringEncoder" class="org.jboss.netty.handler.codec.string.StringEncoder"/>

当我尝试开始我的实现时,我总是得到这个异常:

org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[netty:tcp://localhost:7000?sync=true&kee... because of Failed to resolve endpoint: netty://tcp://localhost:7000?allowDefaultCodec=false&decoder=%23stringDecoder&encoder=%23stringEncoder&keepAlive=true&sync=true due to: Could not find a suitable setter for property: decoder as there isn't a setter method with same type: java.lang.String nor type conversion possible: No type converter available to convert from type: java.lang.String to the required type: org.jboss.netty.channel.ChannelHandler with value #stringDecoder
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:192)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:805)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:2174)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1906)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1767)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1735)
    at com.springapp.mvc.MainService.<init>(MainService.java:70)
    at com.springapp.mvc.MainService$$EnhancerBySpringCGLIB$acc1320.<init>(<generated>)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1763)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
    at javax.management.remote.rmi.RMIConnectionImpl.access0(RMIConnectionImpl.java:97)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
    at sun.rmi.transport.Transport.run(Transport.java:177)
    at sun.rmi.transport.Transport.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

有人可以帮助我吗?是我的错,还是上面的例子不起作用?谢谢。

您需要创建一个基于 spring 的 camel 上下文,例如

CamelContext ccontext = new DefaultCamelContext();

应该是

CamelContext ccontext = new SpringCamelContext(applicationContext);

然后您需要掌握 spring ApplicationContext 以便 Camel 可以与 Spring 集成并在您使用 <bean> 定义时查找 bean。

另一种方法是在 spring xml 文件中设置 <camelContext>,然后为您完成所有这些,然后您仍然可以在 java 代码中定义路由.请参阅 Camel 文档:http://camel.apache.org/spring.html

我知道问这个问题已经 4 年了,但分享解决方案也许仍然有效。 我将 Netty Camel 与 Spring 启动应用程序一起使用,我必须将这两个 bean 添加到 spring 上下文中才能使 sprint 编码器和解码器正常工作。

@Bean(name = "stringDecoder") 
public StringEncoder getStringEncoder() {
    return new StringEncoder();
}

@Bean(name = "stringEncoder") 
public StringDecoder getStringDecoder() {
    return new StringDecoder();
}

有了这些,一切对我来说都很好。