How to test routes in apache camel (spring) - java

This is my system.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<import resource="jetty.xml" />
<import resource="communication.xml" />
<camelContext xmlns="http://camel.apache.org/schema/spring"
id="camel">
<route>
<from uri="file://test.json"/>
<threads>
<bean ref="operationTest" method="test" />
</threads>
</route>
</camelContext>
</beans>
Here is my communication.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="operationTest" class="edu.test.Test">
</bean>
</beans>
I have a main of
context = new SpringServerContext(new FileSystemXmlApplicationContext("conf/system.xml"));
Runtime.getRuntime().addShutdownHook(new Thread(group, new Runnable() {
#Override
public void run() {
stop();
}
}, shutdownThreadName));
context.startup();
and the Test class has a method of
public Object test(Object info) {
//prints info
}
Yet when I run, nothing prints. . .
Can we not trigger the from tag by a file?
I am also trying from Active MQ and is not succeding getting an error of
because of No endpoint could be found for: brokerURL, please check your classpath contains the needed Camel component jar.

I used Active MQ for the route and it worked

Related

Changing Camel REST HTTP Binding Configuration

I'm using Camel REST with Camel servlet handling the REST transport and want to change the way headers from the exchange are processed in HTTP requests and responses. I'm using Spring XML to configure my application. Here's the relevant configuration I'm using:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<!-- Custom Camel Configuration //////////////////////////////////////////////////////////////////////////////// -->
<bean id="myHttpBinding" class="com.example.MyHttpBinding"/>
<bean id="servlet" class="org.apache.camel.component.servlet.ServletComponent">
<property name="httpBinding" ref="myHttpBinding"/>
</bean>
<!-- Routes //////////////////////////////////////////////////////////////////////////////////////////////////// -->
<camel:camelContext id="myCamelContext">
<camel:contextScan/>
<camel:restConfiguration component="servlet" enableCORS="true" bindingMode="json" skipBindingOnErrorCode="false">
<camel:corsHeaders key="Access-Control-Allow-Methods" value="PATCH, PUT, POST, DELETE, GET, OPTIONS, HEAD"/>
<camel:corsHeaders key="Access-Control-Allow-Origin" value="*"/>
<camel:corsHeaders key="Access-Control-Allow-Headers" value="*"/>
</camel:restConfiguration>
</camel:camelContext>
</beans>
When the routes are created, I see that the endpoints are configured with MyHttpBinding set. However, incoming requests are still using ServletRestHttpBinding. This is because when Camel creates the consumer, it executes this block of code:
if (!map.containsKey("httpBinding")) {
// use the rest binding, if not using a custom http binding
HttpBinding binding = new ServletRestHttpBinding();
binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy());
binding.setTransferException(endpoint.isTransferException());
binding.setEagerCheckContentAvailable(endpoint.isEagerCheckContentAvailable());
endpoint.setHttpBinding(binding);
}
How can I set the HTTP binding in a way that Camel will respect it?
Because Camel ultimately looks for the httpBinding property on the endpoint to determine the binding strategy to use, the REST component has to be configured to add that property to the endpoint like so:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<!-- Custom Camel Configuration //////////////////////////////////////////////////////////////////////////////// -->
<bean id="myHttpBinding" class="com.example.MyHttpBinding"/>
<!-- Routes //////////////////////////////////////////////////////////////////////////////////////////////////// -->
<camel:camelContext id="myCamelContext">
<camel:contextScan/>
<camel:restConfiguration component="servlet" enableCORS="true" bindingMode="json" skipBindingOnErrorCode="false">
<camel:endpointProperty key="httpBinding" value="myHttpBinding"/>
<camel:corsHeaders key="Access-Control-Allow-Methods" value="PATCH, PUT, POST, DELETE, GET, OPTIONS, HEAD"/>
<camel:corsHeaders key="Access-Control-Allow-Origin" value="*"/>
<camel:corsHeaders key="Access-Control-Allow-Headers" value="*"/>
</camel:restConfiguration>
</camel:camelContext>
</beans>
Note that I removed the custom servlet component because it wasn't necessary.

Using Profile Properties in ClientInterceptor

I have a profiles.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Local -->
<beans profile="local">
<util:properties id="localProperties">
<prop key="property">localProperty</prop>
</util:properties>
<context:property-placeholder properties-ref="localProperties" ignore-unresolvable="true" />
</beans>
<!-- Dev -->
<beans profile="dev">
<util:properties id="devProperties">
<prop key="property">devProperty</prop>
</util:properties>
<context:property-placeholder properties-ref="devProperties" ignore-unresolvable="true" />
</beans>
</beans>
and I have a org.springframework.ws.client.support.interceptor.ClientInterceptor that I want to use values from profiles.xml:
#Component
public class HeaderInjector implements ClientInterceptor {
#Value("${property}")
private static String someProperty;
#Override
public boolean handleRequest(MessageContext messageContext)
throws WebServiceClientException {
//want to use someProperty here based on value from profiles.xml
}
}
How can I do this? I tried adding #ImportResource("profiles.xml") at the top of the class like
#Component
#ImportResource("profiles.xml")
public class SoapLeadPipeHeaderInjector implements ClientInterceptor {
but someProperty never gets set.
First of all there is nothing about Spring Integration in your question, so be careful with choosing tags for questions.
#ImportResource("profiles.xml") can be applied on the #Configuration class if you start application context from annotations.
If your main entry point is XML configuration, your #Component must be scanned via <context:component-scan base-package="..."/> then.
See more info in the Spring Framework Reference Manual.

Importing Spring bean definition file using relative path

I am writing spring configurations for my project in JAVA. I am new to spring and not to able to figure out the errors related to spring.
I have the following file structure:
ProjectName
--> src
--> META-INF
--> jobs
-->edx
-->request-details.xml
-->clients.xml
-->daos.xml
-->environment.xml
--> request-details-edx-upload.xml
In META-INF/request-details-edx-upload.xml I import the following:
<import resource="jobs/environment.xml" />
<import resource="classpath:META-INF/jobs/edx/request-details.xml" />
<import resource="jobs/clients.xml" />
<import resource="jobs/daos.xml" />
But I get the following error:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/jobs/edx/request-details.xml]
Offending resource: class path resource [META-INF/request-details-edx-upload.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for attribute [dataSetName]
Offending resource: class path resource [META-INF/jobs/edx/request-details.xml]
request-details.xml is something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
context:providerName="scot"
context:subjectName="${edxRequestDetailsSubject}"
context:dataSetName="by-01-day"
context:keyName="${regionValue}"
context:edxClient-ref="edxClient"
/>
<util:list id="requestDetailsColumnMetaData" value-type="com.amazon.edx.transformer.ColumnMetaData">
<ref bean="Column"/>
<ref bean="Column1"/>
</util:list>
<bean id="Column" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="Id"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<bean id="Column1" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="id1"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<util:constant id="tabDelimiter"
static-field="com.amazon.edx.transformer.Delimiters.TAB_DELIMITER" />
<!-- RequestDetails data backup -->
<bean id="requestDetailsDataTransformer" class="com.amazon.edx.transformer.DataTransformerImpl"
context:dataFlattener-ref="requestDetailsDataFlattener"
context:columnMetadata-ref="requestDetailsColumnMetaData"
context:delimiter="{tabDelimiter}"
/>
</beans>
Is there anything obvious I'm doing wrong with my import of the relative path?
Thanks.
Seems it is wrong schema definition. Try to remove namspace xmlns:context="http://www.springframework.org/schema/context" from <beans> tag. Instead add xmlns:p="http://www.springframework.org/schema/p" namespace. Also you should replace all context: suffix with p: suffix. Proper xml using shortcut with the p-namespace should looks like following:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
p:providerName="scot"
p:subjectName="${edxRequestDetailsSubject}"
p:dataSetName="by-01-day"
p:keyName="${regionValue}"
p:edxClient-ref="edxClient"
/>
....
</beans>

Spring Data Populator configuration: org.xml.sax.SAXParseException

I would like to use the Spring Data Populator, but I am getting the SAXParseException:
Exception in thread "main"
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Failed to import bean definitions from URL location
[classpath*:spring-context/dbContext.xml]
Offending resource: class path resource [spring-context/applicationContext.xml];
nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 64 in XML document from URL [file:/home/pi/Workspace/AGServer/bin/spring-context/dbContext.xml] is invalid;
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.3.2.2:
Attribute 'location' is not allowed to appear in element 'repository:jackson-populator'.
My main server class is:
public final class ServerMain {
private static final String APP_CONTEXT_XML = "spring-context/applicationContext.xml";
public static void main(final String[] args) {
final ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
context.registerShutdownHook();
}
}
The applicationContext.xml loads other sub-files and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath*:spring-context/serverContext.xml" />
<import resource="classpath*:spring-context/dbContext.xml" />
<import resource="classpath*:spring-context/jettyContext.xml" />
</beans>
And finally, the beginning of the dbContext.xml from where I invoke the Populator
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd">
<!-- AUTH DS / PU / TX / SERVICE -->
<!-- the repositories configuration, ommited for clarity -->
<repository:jackson-populator location="classpath:auth.json" />
May that be caused somehow by conflicts with spring-beans-3.0.xsd? I saw in all (though not too many, e.g. here: http://docs.spring.io/spring-data/commons/docs/1.5.1.RELEASE/reference/htmlsingle/???#d0e893) documents that spring-beans.xsd, without any release id is used. I tried to change it, but that didn't help.

Injecting property file values into Controller via Spring not working

Hoping you may be able to help. My Spring knowledge is extremely limited to apologies if the solution is something really obvious.
Here are the contents of the relevant files:
application-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<util:properties id="messageConfig" location="classpath:customMessages.properties"/>
</beans>
/src/main/resources/customMessages.properties:
sms.message=Test
Java class:
#Controller
public class ApiRestController implements ApiRest {
#Value("#{messageConfig['sms.message']}")
private String smsMessage;
public final void setSmsMessage (String smsMessage)
{
this.smsMessage = smsMessage;
}
...
}
However, when referencing the 'smsMessage' variable elsewhere in the Java class, the value is NULL. Any ideas what I might be missing? Any pointers would be really appreciated.
Add
<context:component-scan base-package="spring-controllers-package"/>
to the application-context.xml

Categories

Resources