Camel JMSComponent未在JUnit中注册无法解析端点

时间:2017-11-14 10:21:06

标签: java spring junit apache-camel activemq

Iam尝试在Junit测试用例中向JMSQueue发送消息,并且我获得异常没有找到使用scheme的组件:hornetq。我已经在bean声明中注册了组件。但它仍然是抛出错误。

Junit测试课程:

import java.io.File;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(CamelSpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:/META-INF/spring/camel-context.xml",
        "classpath:/META-INF/spring/policyHolderRoutepolicyHolderRoute.xml", "classpath:/META-INF/spring/policyRoute.xml" })
public class OnlineComponentTest extends CamelTestSupport {
    private static final Logger LOGGER = LoggerFactory.getLogger(OnlineComponentTest.class);
    @Autowired
    private CamelContext context;

    @Produce(uri = "hornetq://queue:EVENT.API.IN.ONLINE")
    protected ProducerTemplate template;

    @EndpointInject(uri = "mock:cxfrs://bean://importPolicyHolderRestEndpoint")
    protected MockEndpoint mockImportpolicyHolderEndpoint;

    @EndpointInject(uri = "mock:cxfrs://bean://importPolicyRestEndpoint")
    protected MockEndpoint mockImportpolicyEndpoint;

    @Test
    public void testOnlineRouteSuccess() throws Exception {
        template.sendBody("hornetq://queue:EVENT.API.IN.ONLINE", FileUtils.readFileToString(new File(
                "src/test/resources/componenttest/success/input/onlineinqueue-request.json")));
    }
}

在测试资源中使用HornetQ定义组件:

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="vm://broker?broker.persistent=false" />
    </bean>

    <!-- setup spring jms TX manager -->
    <bean id="jmsTransactionManager"
        class="org.springframework.jms.connection.JmsTransactionManager">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>

    <bean id="hornetq" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <!-- define the jms consumer/producer as transacted -->
        <property name="transacted" value="true" />
        <!-- setup the transaction manager to use -->
        <!-- if not provided then Camel will automatic use a JmsTransactionManager, 
            however if you for instance use a JTA transaction manager then you must configure 
            it -->
        <property name="transactionManager" ref="jmsTransactionManager" />
    </bean>

Junit Logs:

2017-11-14 09:57:42,559 [main           ]  INFO  XmlBeanDefinitionReader        - Loading XML bean definitions from class path resource [META-INF/spring/camel-context.xml]
2017-11-14 09:57:43,186 [main           ]  INFO  XmlBeanDefinitionReader        - Loading XML bean definitions from class path resource [META-INF/spring/bean-declarations.xml]
2017-11-14 09:57:45,984 [main           ]  INFO  DefaultListableBeanFactory     - Overriding bean definition for bean 'OnlineInEndpoint': replacing [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-11-14 09:57:45,985 [main           ]  INFO  DefaultListableBeanFactory     - Overriding bean definition for bean '_to2': replacing [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-11-14 09:57:45,986 [main           ]  INFO  DefaultListableBeanFactory     - Overriding bean definition for bean '_to1': replacing [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.apache.camel.spring.CamelEndpointFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-11-14 09:57:45,990 [main           ]  INFO  XmlBeanDefinitionReader        - Loading XML bean definitions from class path resource [META-INF/spring/policyHolderRoute.xml]
2017-11-14 09:57:46,099 [main           ]  INFO  XmlBeanDefinitionReader        - Loading XML bean definitions from class path resource [META-INF/spring/policyRoute.xml]
2017-11-14 09:57:46,202 [main           ]  INFO  CamelSpringTestContextLoader   - Disabling Camel JMX globally for tests by default.  Use the DisableJMX annotation to override the default setting.
2017-11-14 09:57:46,257 [main           ]  INFO  GenericApplicationContext      - Refreshing org.springframework.context.support.GenericApplicationContext@71318ec4: startup date [Tue Nov 14 09:57:46 GMT 2017]; root of context hierarchy
2017-11-14 09:57:46,711 [main           ]  INFO  ePropertyPlaceholderConfigurer - Loading properties file from class path resource [com/company/esb/environment/properties/common.properties]
2017-11-14 09:57:46,712 [main           ]  INFO  ePropertyPlaceholderConfigurer - Loading properties file from class path resource [com/company/esb/environment/properties/online.properties]
2017-11-14 09:57:46,726 [main           ]  INFO  redAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-11-14 09:57:46,780 [main           ]  INFO  DefaultListableBeanFactory     - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@41dd05a: defining beans [eventLogger,bridgePropertyPlaceholder,jmsConnectionFactory,jmsTransactionManager,hornetq,importPolicyRestEndpoint,importPolicyHolderRestEndpoint,*.http-conduit,OnlineInEndpoint,_to2,_to1,template,consumerTemplate,OnlineContext:beanPostProcessor,OnlineContext,policyHolderRouteContext,policyRouteContext,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2017-11-14 09:57:47,913 [main           ]  INFO  CamelContextFactoryBean        - Bridging Camel and Spring property placeholder configurer with id: bridgePropertyPlaceholder
2017-11-14 09:57:48,777 [main           ]  INFO  DefaultTypeConverter           - Loaded 201 type converters
2017-11-14 09:57:48,799 [main           ]  INFO  CamelSpringTestContextLoader   - Setting shutdown timeout to [10 SECONDS] on CamelContext with name [OnlineContext].
2017-11-14 09:57:48,811 [main           ]  INFO  CamelSpringTestContextLoader   - Enabling lazy loading of type converters on CamelContext with name [OnlineContext].
2017-11-14 09:57:48,813 [main           ]  INFO  CamelSpringTestContextLoader   - Starting CamelContext with name [OnlineContext].
2017-11-14 09:57:48,815 [main           ]  INFO  SpringCamelContext             - Apache Camel 2.17.0.redhat-630262 (CamelContext: OnlineContext) is starting
2017-11-14 09:57:48,821 [main           ]  INFO  DefaultManagementStrategy      - JMX is disabled
2017-11-14 09:57:50,991 [main           ]  INFO  SpringCamelContext             - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2017-11-14 09:57:51,147 [main           ]  INFO  DefaultStreamCachingStrategy   - StreamCaching in use with spool directory: C:\Users\jmjl29k\AppData\Local\Temp\camel\camel-tmp-8dce384f-0ca4-4e09-9560-53a30d7b089f and rules: [Spool > 128K body size]
2017-11-14 09:57:51,222 [main           ]  INFO  SpringCamelContext             - Route: policyRoute started and consuming from: Endpoint[direct://policyLoad]
2017-11-14 09:57:51,223 [main           ]  INFO  SpringCamelContext             - Route: policyHolderRoute started and consuming from: Endpoint[direct://policyHolderLoad]
2017-11-14 09:57:51,237 [main           ]  INFO  SpringCamelContext             - Route: OnlineInWriteConsumerRoute started and consuming from: Endpoint[hornetq://queue:EVENT.API.IN.ONLINE]
2017-11-14 09:57:51,237 [main           ]  INFO  SpringCamelContext             - Total 3 routes, of which 3 are started.
2017-11-14 09:57:51,240 [main           ]  INFO  SpringCamelContext             - Apache Camel 2.17.0.redhat-630262 (CamelContext: OnlineContext) started in 2.424 seconds
2017-11-14 09:57:51,450 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,450 [main           ]  INFO  OnlineComponentTest            - Testing: testOnlineRouteSuccess(com.company.esb.online.OnlineComponentTest)
2017-11-14 09:57:51,450 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,605 [.API.IN.ONLINE]]  INFO  BrokerService                  - Using Persistence Adapter: MemoryPersistenceAdapter
2017-11-14 09:57:51,610 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,622 [main           ]  INFO  OnlineComponentTest            - Testing done: testOnlineRouteSuccess(com.company.esb.online.OnlineComponentTest)
2017-11-14 09:57:51,622 [main           ]  INFO  OnlineComponentTest            - Took: 9.366 seconds (9366 millis)
2017-11-14 09:57:51,622 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,645 [main           ]  INFO  DefaultCamelContext            - Apache Camel 2.17.0.redhat-630262 (CamelContext: camel-2) is shutting down
2017-11-14 09:57:51,646 [main           ]  INFO  DefaultCamelContext            - Apache Camel 2.17.0.redhat-630262 (CamelContext: camel-2) uptime 
2017-11-14 09:57:51,646 [main           ]  INFO  DefaultCamelContext            - Apache Camel 2.17.0.redhat-630262 (CamelContext: camel-2) is shutdown in 0.023 seconds
2017-11-14 09:57:51,647 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,647 [main           ]  INFO  OnlineComponentTest            - Testing done: testOnlineRouteSuccess(com.company.esb.online.OnlineComponentTest)
2017-11-14 09:57:51,647 [main           ]  INFO  OnlineComponentTest            - Took: 0.197 seconds (197 millis)
2017-11-14 09:57:51,647 [main           ]  INFO  OnlineComponentTest            - ********************************************************************************
2017-11-14 09:57:51,647 [main           ]  INFO  GenericApplicationContext      - Closing org.springframework.context.support.GenericApplicationContext@71318ec4: startup date [Tue Nov 14 09:57:46 GMT 2017]; root of context hierarchy
2017-11-14 09:57:51,649 [main           ]  INFO  SpringCamelContext             - Apache Camel 2.17.0.redhat-630262 (CamelContext: OnlineContext) is shutting down
2017-11-14 09:57:51,651 [main           ]  INFO  DefaultShutdownStrategy        - Starting to graceful shutdown 3 routes (timeout 10 seconds)
2017-11-14 09:57:51,889 [JMX connector  ]  INFO  ManagementContext              - JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
2017-11-14 09:57:52,063 [.API.IN.ONLINE]]  INFO  BrokerService                  - Apache ActiveMQ 5.11.0.redhat-630262 (broker, ID:VPE1-D01307-58386-1510653471708-0:1) is starting
2017-11-14 09:57:52,084 [.API.IN.ONLINE]]  INFO  BrokerService                  - Apache ActiveMQ 5.11.0.redhat-630262 (broker, ID:VPE1-D01307-58386-1510653471708-0:1) started
2017-11-14 09:57:52,084 [.API.IN.ONLINE]]  INFO  BrokerService                  - For help or more information please see: http://activemq.apache.org
2017-11-14 09:57:52,105 [.API.IN.ONLINE]]  WARN  BrokerService                  - Temporary Store limit is 51200 mb (current store usage is 0 mb). The data directory: C:\Temp\GITRepo\esb\routes\Online only has 26504 mb of usable space. - resetting to maximum available disk space: 26504 mb
2017-11-14 09:57:52,195 [.API.IN.ONLINE]]  INFO  TransportConnector             - Connector vm://broker started
2017-11-14 09:57:53,612 [.API.IN.ONLINE]]  INFO  TransportConnector             - Connector vm://broker stopped
2017-11-14 09:57:53,613 [.API.IN.ONLINE]]  INFO  BrokerService                  - Apache ActiveMQ 5.11.0.redhat-630262 (broker, ID:VPE1-D01307-58386-1510653471708-0:1) is shutting down
2017-11-14 09:57:53,634 [.API.IN.ONLINE]]  INFO  BrokerService                  - Apache ActiveMQ 5.11.0.redhat-630262 (broker, ID:VPE1-D01307-58386-1510653471708-0:1) uptime 2.167 seconds
2017-11-14 09:57:53,634 [.API.IN.ONLINE]]  INFO  BrokerService                  - Apache ActiveMQ 5.11.0.redhat-630262 (broker, ID:VPE1-D01307-58386-1510653471708-0:1) is shutdown
2017-11-14 09:57:53,634 [ - ShutdownTask]  INFO  DefaultShutdownStrategy        - Route: OnlineInWriteConsumerRoute shutdown complete, was consuming from: Endpoint[hornetq://queue:EVENT.API.IN.ONLINE]
2017-11-14 09:57:53,635 [ - ShutdownTask]  INFO  DefaultShutdownStrategy        - Route: policyHolderRoute shutdown complete, was consuming from: Endpoint[direct://policyHolderLoad]
2017-11-14 09:57:53,635 [ - ShutdownTask]  INFO  DefaultShutdownStrategy        - Route: policyRoute shutdown complete, was consuming from: Endpoint[direct://policyLoad]
2017-11-14 09:57:53,635 [main           ]  INFO  DefaultShutdownStrategy        - Graceful shutdown of 3 routes completed in 1 seconds
2017-11-14 09:57:53,640 [main           ]  INFO  SpringCamelContext             - Apache Camel 2.17.0.redhat-630262 (CamelContext: OnlineContext) uptime 4.827 seconds
2017-11-14 09:57:53,640 [main           ]  INFO  SpringCamelContext             - Apache Camel 2.17.0.redhat-630262 (CamelContext: OnlineContext) is shutdown in 1.991 seconds
2017-11-14 09:57:53,643 [main           ]  INFO  DefaultListableBeanFactory     - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@41dd05a: defining beans [eventLogger,bridgePropertyPlaceholder,jmsConnectionFactory,jmsTransactionManager,hornetq,importPolicyRestEndpoint,importPolicyHolderRestEndpoint,*.http-conduit,OnlineInEndpoint,_to2,_to1,template,consumerTemplate,OnlineContext:beanPostProcessor,OnlineContext,policyHolderRouteContext,policyRouteContext,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

例外:

org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: hornetq://queue:EVENT.API.IN.ONLINE due to: No component found with scheme: hornetq
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:595)
    at org.apache.camel.util.CamelContextHelper.getEndpointInjection(CamelContextHelper.java:174)
    at org.apache.camel.impl.CamelPostProcessorHelper.doGetEndpointInjection(CamelPostProcessorHelper.java:157)
    at org.apache.camel.impl.CamelPostProcessorHelper.getEndpointInjection(CamelPostProcessorHelper.java:152)
    at org.apache.camel.impl.CamelPostProcessorHelper.createInjectionProducerTemplate(CamelPostProcessorHelper.java:289)
    at org.apache.camel.impl.CamelPostProcessorHelper.getInjectionValue(CamelPostProcessorHelper.java:205)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor.injectField(DefaultCamelBeanPostProcessor.java:195)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor$1.doWith(DefaultCamelBeanPostProcessor.java:187)
    at org.apache.camel.util.ReflectionHelper.doWithFields(ReflectionHelper.java:73)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor.injectFields(DefaultCamelBeanPostProcessor.java:168)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor.postProcessBeforeInitialization(DefaultCamelBeanPostProcessor.java:82)
    at org.apache.camel.test.junit4.CamelTestSupport.applyCamelPostProcessor(CamelTestSupport.java:499)
    at org.apache.camel.test.junit4.CamelTestSupport.postProcessTest(CamelTestSupport.java:487)
    at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:320)
    at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:239)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用2.20.x,因为它改进了Camel和Spring的启动。但您也可以在当前版本中避免在@Produce注释

中使用自定义组件名称

然后只是引用一个存在于camel-core中的uri

 @Produce(uri = "log:foo")

在您的测试方法中,您发送到hornetq,此时此时应该正常工作,因为Spring已完全启动并使用自定义<bean>加载XML文件。

相关问题