convert apache camel config to spring java config - spring

we are in the process of converting current spring project into spring boot and at the same time converting all spring beans from xml to java config based.
i am stuck converting camel xml configuration into java based config.
currently we are specified camel config , routes and endpoints , one example as below
<camel:camelContext id="camelClient">
<camel:template id="camelTemplate"/>
</camel:camelContext>
<template id="camelTemplate"/>
here are couple of endpoints
<endpoint id="archiveUserQueue"
uri="swiftmq:${hk.jms.archive.queue.name}?concurrentConsumers=${hk.jms.archive.queue.consumers}"/>
<endpoint id="directSmsNotification" uri="direct:sendSMS"/>
one of the routes defined
<route>
<from ref="directSmsNotification"/>
<to uri="bean:messengerService?method=sendSmsMessage"/>
</route>
in java code we access the end point as below
smsEndpoint = _camelContext.getEndpoint("directSmsNotification");
how can we convert the camel config from xml to java based config.
i have followed instructions specified at http://camel.apache.org/spring-java-config.html but it was too hard to understand as i am not familiar with Camel.

You can mix and match Spring Java config with Apache Camel XML config. I question why you're doing this conversion in the first place.
That said, if you look at the camel docs you'll see there's an example for working with RouteBuilder.
You could also look at the sample spring-boot application. Here's a modified RouteBuilder from that example:
#Component
public class MySpringBootRouter extends RouteBuilder {
#Override
public void configure() {
Context context = getContext();
MyEndpoint ep = context.getEndpoint("someURI", MyEndpoint.class);
from(ep)
.transform().simple("ref:myBean")
.to("log:out");
}
}
Update: I modified the snippet to show getting an Endpoint directly. You can get more info in the Camel docs. I'm not sure how common this approach is. Back when I was using Camel regularly the endpoints were configured declaratively through their URI values. I don't think I ever explicitly defined an endpoint in my Camel XML or Java code. I'm sure there are use cases for it but it might be simpler for you to configure just by URI.

Related

Spring Boot + Jersey + view controller not working together [duplicate]

I am getting a HTTP 404 error when trying to serve index.html ( located under main/resources/static) from a spring boot app. However if I remove the Jersey based JAX-RS class from the project, then http://localhost:8080/index.html works fine.
The following is main class
#SpringBootApplication
public class BootWebApplication {
public static void main(String[] args) {
SpringApplication.run(BootWebApplication.class, args);
}
}
I am not sure if I am missing something here.
Thanks
The problem is the default setting of the Jersey servlet path, which defaults to /*. This hogs up all the requests, including request to the default servlet for static content. So the request is going to Jersey looking for the static content, and when it can't find the resource within the Jersey application, it will send out a 404.
You have a couple options around this:
Configure Jerse runtime as a filter (instead of as a servlet by default). See this post for how you can do that. Also with this option, you need to configure one of the ServletProperties to forward the 404s to the servlet container. You can use the property that configures Jersey to forward all request which results in a Jersey resource not being found, or the property that allows you to configure a regex pattern for requests to foward.
You can simply change the Jersey servlet pattern to something else other than the default. The easiest way to do that is to annotate your ResourceConfig subclass with #ApplicationPath("/root-path"). Or you can configure it in your application.properties - spring.jersey.applicationPath.

Start with Apache Camel

I’m very new to Apache Camel and will very appreciate if someone could provide me what camel components may be used to solve particular task.
I have a simple REST WS. This service is not accessible to audience.
The idea is to build middle layer between user requests and endpoint service.
So I will have to catch user’s request, make some manipulations with it, send to restricted WS and give a response to user.
I’m just started learning apache camel and the question is what is the best way to implement this logic.
Thx in advance!
Frankly, Camel is not the right framework to implement web controllers. Of course there is the Camel Rest Module, but it is stretching the responsibilities of the framework too far.
I recommend using a more adapt framework for the implementation of the WS, e.g. Spring or Jersey, and call Camel endpoints programmatically from the request handlers. Within Spring, triggering Camel Endpoints is easy since the CamelContext can get autowired into your web controller:
camelContext.createProducerTemplate().sendBodyAndHeader("direct:myEndpoint", null, "id", id);
For your Camel Root this approach means, that it starts of with a Direct endpoint, then forwards to a Camel http endpoint and if necessary forwards the output from the HTTP call to some Spring bean transformation step, before finally passing it back to the web controller handler method:
<route>
<from uri="direct:myEndpoint"/>
<to uri="http:somehost.com"/>
<transform>
<method ref="springBean" method="doSomeTransformation"/>
</transform>
</route>
Well there are several camel components you can use for this task. Think of Camel as a toolbox where you can choose from several tools for the same task.
You can use:
Camel-HTTP4 http://camel.apache.org/http4.html
Camel-Jetty http://camel.apache.org/jetty.html
Camel-Restlet http://camel.apache.org/restlet.html
Camel-CXFRS http://camel.apache.org/cxfrs.html
Example using java dsl:
from("jetty://http://localhost:7070/test").to("jetty://http://localhost:7070/test1");
Example using blueprint
<route>
<from uri="jetty://http://localhost:7070/test"/>
<to uri="jetty://http://localhost:7070/test1"/>
<route>

Apache CXF Spring Java Config for JAXWS Endpoint

I'm trying to configure Spring with Apache CXF using java config (no XML config) and wanted to know how to register JAXWS endpoints using spring java config. For example, what would be the 'java config' equivalent for the XML config below?
<jaxws:endpoint id="reportService" implementor="#reportServ" address="/reportService"/>
Kind regards,
Zahanghir
The 'Java-config' equivalent of your XML configuration is something like :
#Configuration
public class CXFConfiguration {
#Autowired
private ReportService reportServ;
#Bean
public Endpoint endpoint() {
Endpoint endpoint = new EndpointImpl(reportServ);
endpoint.publish("/reportService");
return endpoint;
}
}
I hope this can help you ^^.
Unfortunately, from what I can tell KevinHol's answer doesn't actually work. A working answer can be found at the sister thread (Apache CXF + Spring Java config (no XML)).

Spring Integration - How to create an optional jms:message-driven-channel-adapter?

I'm looking for a way to conditionally set up jms:message-driven-channel-adapter in spring 3.0 & spring integration 2.2.
I would like to have an entry in a property file like: "create.message.driven.channel.adapter=true" for each environment and I would like spring to decide whether to set up the channel or not based solely on the entry from the property file.
Is there a way to accomplish this using only spring xml configuration and a property file?
You can't do it exactly the way you describe. With Spring 3.1, you could do it with Spring Profiles...
<beans>
...
<beans profile="foo">
<jms:message-driven-adapter ... />
</beans>
</beans>
Then run with ... -Dspring.profiles.active=foo.
You could do it with JavaConfig
#Bean
public Object foo() {
// if property set, return an MDA, otherwise a String
}
Or, probably the easiest, so long as you don't explicitly start() the context, you could use
<jms:message-driven-adapter ...
auto-startup="${start.message.driven.channel.adapter}" />
In which case, the bean would be defined, but it just wouldn't be started so it wouldn't even open a JMS connection. You would also need a property placeholder configurer pointed at your properties file.
But, auto-startup only applies to starting on refresh(), an explicit context.start() will still start it.

Axis2(aar) + spring, without a servletContext

Greetings dear Stackoverflow users, I have been lately in lots of pain with one specific problem with axis2 web services with Spring framework. I have read lots of different guides and read different forums but found people with the same problems but with no solutions. Basically ended up holding the monitor with both of my hands and yelling "What did you find out BudapestHacker938?". Anyway my axis2 web service class needs Spring beans and therefore they are autowired inside the web service class. Everything works so well inside the jetty server where I have servletContext. Just define needed listeners in web.xml and it works. Such a bliss. But unfortunately all good things come to the end in some point, for me, the devil is CICS environment inside of mainframe. There is no servletcontext like in Jetty/Tomcat, luckily it still has axis2 support. So according to the different user-guides I decided to archive my web-service into .aar and added it under the services folder. Axis2 folder structure is the following:
repository/
modules
services
When I am building this .aar archive then I am also generating my own wsdl, not using axis2 inbuilt wsdl generator which according to services.xml generates the services out of the given class (when I am running the axis2server, not using because doesn't like JAX-WS annotations as far as I know). To initialize Spring framework, I needed to write little SpringInit class which initializes Spring beans. Unfortunately it also for some reason initializes my web-service class according to its annotations and then occupies the main port(suspect that SpringInit intializes by its own the web service class since it is also defined as a Spring bean and SpringInit extends Axis2 class ServiceLifeCycle) and I get JVM BIND exception where it is stating that address is already in use. I would like to have the service built up according to the wsdl which is stored inside of the WSDL rather than generate new one, because I have various environments: 1) local machine - Jetty 2) mainframe. Anyway I give an insight to my services.xml:
<service name="Absence" class="org.services.SpringInit">
<description>
random description
</description>
<parameter name="ServiceTCCL">composite</parameter>
<parameter name="useOriginalwsdl" locked="false">true</parameter>
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name="ServiceClass">org.services.Absence</parameter>
<parameter name="SpringBeanName">absence</parameter>
<parameter name="SpringContextLocation">META-INF/applicationContextAar.xml</parameter>
</service>
Spring applicationContextAar.xml, little bit refactored it for dear Stack community:
<beans>
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost/~/devDb" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="absence" class="org.services.Absence"></bean>
<bean id="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="ds"></constructor-arg>
</bean>
<bean id="datasetFactory" class="org.vsam.DataSetFactory"></bean>
<bean id="dataManagerFactory" class="org.datamanager.DataManagerFactory"></bean>
<bean id="absenceFactory" class="org.services.AbsenceFactory"></bean>
<bean id="h2Database" class="org.dataset.H2Database"><constructor-arg ref="jtemplate"></constructor-arg>
</bean>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"></bean>
</beans>
My SpringInit class looks something like that:
public class SpringInit implements ServiceLifeCycle {
public void startUp(ConfigurationContext ignore, AxisService service) {
try {
ClassLoader classLoader = service.getClassLoader();
ClassPathXmlApplicationContext appCtx = new
ClassPathXmlApplicationContext(new String[] {"applicationContextAar.xml"}, false);
appCtx.setClassLoader(classLoader);
appCtx.refresh();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void shutDown(ConfigurationContext ctxIgnore, AxisService ignore) {}
}
Now we are moving to org.services.Absence.class, it is an ordinary JAX-WS web-service class with following header (contains JAX-WS annotations):
#WebService(name = "AbsenceService", serviceName = "Absence", portName = "Absence",
targetNamespace = "http://www.something.org/Absence")
public class Absence extends ServiceHandlerBase {
#Autowired
private AbsenceFactory absenceFactory;
#Autowired
private DataManagerFactory dataManagerFactory;
#Autowired
private DataSetFactory dataSetFactory;
...
}
Containing methods like that:
#WebMethod
#WebResult(name = "AbsenceResponse")
public SearchAbsenceRecordsResponse invokeSearchAbsenceRecords(
#WebParam ServiceRequest request,
#WebParam SearchAbsenceRecordsRequest absenceRequest) {...}
One alternative is to add "servicejars" folder into "repository" folder and populate it with absence.jar which has all its dependencies in the sub-folder "lib". Axis2 then automatically runs absense.jar since it has JAX-WS annotation. But in there when I call out the web-service for example with SOAP-UI, it doesn't have Spring initialized since I don't know how to initialize Spring in that solution. Maybe someone has any expertise about that.
TL;DR
How do I get my Spring beans initialized in manner that it doesn't start the services in the web service class according to the annotation and would rather build up services according to the wsdl?
You are welcome to ask questions.
How I initialized Spring inside of CICS without servletcontext?
Basically until today the SOAP web services have been published through servicejars which means into the repository folder has been created "servicejars" folder which cointains jars which have been built from the web service classes. "servicejars" subfolder "lib" contains all the dependencies which web service jars need.
At first I learnt from the web(Axis2 homepage, there was an instruction about axis2 and spring integration) for initializing Spring in Axis2 web service I need .aar archive and SpringInit service defined in services.xml. But this brought lots of problems since having old architecture built on jaxws and jaxb there was a huge need for refactoring the web services layer. Axis2 tolerated jaxws annotations only with "servicejars" solution. Initing Spring with SpringInit class meant that it initializes Spring beans according to the application context. This now runs web service bean(absence bean in previous post) as a separate web service and occupied 8080 port, when time came for the web service creation according to WSDL I got an error "JVM bind address already in use". So after that I figured I should create the service according to the absence Spring bean and let axis2server generate the WSDL, but axis2server didn't like jaxws annotation and even without them it didn't like my jaxb DTOs.
Therefore, I decided to drop .aar architecture and went back to the "servicejars" architecture. Unfortunately in there I didn't have services.xml support, to define the potential SpringInit service.
Since jaxws web services are the only entrypoints then I decided do the following (initialize Spring beans in the web service layer):
#WebService(name = "AbsenceService", serviceName = "Absence", portName = "Absence",
targetNamespace = "http://www.something.org/Absence")
public class Absence extends ServiceHandlerBase {
private static AbsenceFactory absenceFactory;
private static DataManagerFactory dataManagerFactory;
private static DataSetFactory dataSetFactory;
static {
try {
ClassPathXmlApplicationContext appCtx = new
ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, false);
appCtx.refresh();
absenceFactory = (AbsenceFactory) appCtx.getBean("absenceFactory", AbsenceFactory.class);
dataManagerFactory = (DataManagerFactory) appCtx.getBean("dataManagerFactory", DataManagerFactory.class);
dataSetFactory = (DataSetFactory) appCtx.getBean("datasetFactory", DataSetFactory.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
...
}
As you can see when this class is being called out, it will initialize applicationcontext and since it is static, all the spring beans will stay in the memory until the end(when service is closed). In other classes autowiring works perfectly, no need to get these beans wired manually.
In the end, I didn't find the possiblity to initialize Spring in the matter as I hoped through .aar architecture, but I found a work around with the guidance of a senior programmer. Huge thanks to him! And now the possible solution is visible for all StackOverFlow users.
EDIT:
In applicationContext.xml I had:
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"/>
Tries to create web services with Absence.class(absence bean). Removed it since I can in local machine as well use pre-generated WSDL with Jetty (originally was used for creating web service in the local machine, like I said before, I have local development environment and it should be also compatible with CICS, now it is solved).

Resources