I am using Camel in Karaf with blueprint xml and I want to create a bean with the class coming from a different bundle.
<blueprint>
...
<bean id="token-validation" class="com.xxx.security.JwtTokenValidator" init-method="init" >
<property name="realm" value="${auth.realm}"/>
</bean>
...
<camelContext>
<route id="route.EventsNotify" routePolicyRef="token-validation">
...
</camelContext>
</blueprint>
JwtTokenValidator class is located in another bundle, that extends the Camel's RoutePolicySupport, that's why it is applied in route route.EventsNotify.
public class JwtTokenValidator extends RoutePolicySupport {
#Override
public void onExchangeBegin(Route route, Exchange exchange) {
super.onExchangeBegin(route, exchange);
checkAuthorizationHeader(exchange);
}
...
}
This bundle has some dependencies and classes like the aforementioned one, in order to be used in many projects. Write once and applied in many projects instead of coping the same code again and again.
Unfortunately this is not working, I am getting the following error in stacktrace
Caused by: java.lang.ClassCastException: Cannot cast com.xxx.security.JwtTokenValidator to org.apache.camel.spi.RoutePolicy
I cannot understand why, because the class is extending RoutePolicySupport which in terms implements the desired RoutePolicy interface.
If I move the class to the same bundle it is working, but I need to have it in a seperate bundle for the reasons that I explained before.
Could someone tells me where I am wrong?
Thanks a lot!
Try to import all required Classes (org.apache.camel.spi.RoutePolicy, ...) to current Bundle's classLoader. If you are building your bundle with maven-bundle plugin, you can do it like this:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
...
<configuration>
<instructions>
...
<Import-Package>
...,
org.apache.camel.spi*;version=[min_version, max_version),
...
</Import-Package>
</instructions>
</configuration>
</plugin>
Related
I am writing a test for Camel using Spring boot.
Below is configuration on the test class
#RunWith(CamelSpringBootRunner.class)
#SpringBootApplication
#ComponentScan(basePackages = ["example.test"])
#UseAdviceWith
#BootstrapWith(SpringBootTestContextBootstrapper)
#DirtiesContext
class RouteTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RouteTest.class)
#Autowired ModelCamelContext camelContext
#Test
void "flow"() {
camelContext.getRouteDefinition(route.routeId).adviceWith(camelContext, new AdviceWithRouteBuilder() {
#Override
void configure() throws Exception {
}
}
LOGGER.info("IN TEST: ******* Camel Status: "+camelContext.getStatus())
}
I expect camel should not be started. But when I run the test it is already started.
I noticed that CamelSpringBootRunner does start camel context in CamelSpringBootExecutionListener.
How do I force not to start the camel context.
In the latest version of camel there is an option for autoStartup of camel. You an achieve what you want by adding autoStartup option.
For example the route below is configured autoStartup=false to prevent Camel starting when Spring starts.
<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
<route>
<from uri="direct:start"/>
<to uri="mock:result"/>
</route>
</camelContext>
You can manually start Camel later by invoking its start method as shown below:
ApplicationContext ac = ...
SpringCamelContext camel = (SpringCamelContext) ac.getBean("myCamel");
// now start Camel manually
camel.start();
If you are using older version of camel then autoStartup option will not work try using shouldStartContext instead.
Sometimes starting camel after setting shouldStartContext doesn't work so I have put the work around in below example. Try this :
setting shouldStartContext manually before starting the context from the code:
((SpringCamelContext)camelContext).setShouldStartContext(true);
camelContext.start();
Example context:
<camel:camelContext id="ids.camel.context" shouldStartContext="false">
<!-- Queue endpoints. -->
<camel:endpoint id="defaultInQueue" uri="jms:queue:${default.in.queue.name}"/>
<camel:endpoint id="defaultOutQueue" uri="jms:queue:${default.out.queue.name}"/>
<!-- Route to send messages to IDS -->
<camel:route id="out" trace="true">
<camel:from uri="direct:sender"/>
<!-- Do not expect answer (fire and forget) -->
<camel:setExchangePattern pattern="InOnly"/>
<camel:to ref="defaultOutQueue"/>
</camel:route>
<!-- Route to receive -->
<camel:route id ="in" trace="true">
<camel:from ref="defaultInQueue"/>
<camel:to uri="bean:defaultTextAdapter?method=onMessage"/>
</camel:route>
</camel:camelContext>
Using Camel 2.20.1 solved the issue
I am trying to create simple camel-test-blueprint, but can not proceed. I am able to do normal camel-test with the routes , but when I am trying with camel-test-blueprint I am getting exception. I think Some configuration is missing. I have created this test cases by referring Apache camel site only, but it's not working. Something is missing.
my POM:
<properties>
<camel-version>2.17.0</camel-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-blueprint</artifactId>
<version>${camel-version}</version>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>2.4.0</version>
</plugin>
</plugins>
</build>
My test class:
package com.test.routes;
import org.apache.camel.Exchange;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
import org.junit.Test;
//tag::example[]
//to use camel-test-blueprint, then extend the CamelBlueprintTestSupport class,
//and add your unit tests methods as shown below.
public class DebugBlueprintTest extends CamelBlueprintTestSupport {
private boolean debugBeforeMethodCalled;
private boolean debugAfterMethodCalled;
// override this method, and return the location of our Blueprint XML file to be used for testing
#Override
protected String getBlueprintDescriptor() {
return "OSGI-INF/blueprint/route.xml";
}
// here we have regular JUnit #Test method
#Test
public void testRoute() throws Exception {
System.out.println("*** Entering testRoute() ***");
// set mock expectations
//getMockEndpoint("mock:a").expectedMessageCount(1);
getMockEndpoint("mock:vm:inputFile1").expectedMessageCount(1);
// send a message
//template.sendBody("direct:start", "World");
template.sendBody("vm:inputFileEndpointTest", "Hello World");
// assert mocks
assertMockEndpointsSatisfied();
// assert on the debugBefore/debugAfter methods below being called as we've
enabled the debugger
assertTrue(debugBeforeMethodCalled);
assertTrue(debugAfterMethodCalled);
}
#Override
public boolean isUseDebugger() {
// must enable debugger
return true;
}
#Override
protected void debugBefore(Exchange exchange, org.apache.camel.Processor processor, ProcessorDefinition<?> definition, String id, String label) {
log.info("Before " + definition + " with body " + exchange.getIn().getBody());
debugBeforeMethodCalled = true;
}
#Override
protected void debugAfter(Exchange exchange, org.apache.camel.Processor processor, ProcessorDefinition<?> definition, String id, String label, long timeTaken) {
log.info("After " + definition + " with body " + exchange.getIn().getBody());
debugAfterMethodCalled = true;
}
}
//end::example[]
when I am trying to run this, I am getting below exception:
java.lang.IncompatibleClassChangeError: Class org.apache.felix.connect.felix.framework.ServiceRegistrationImpl$ServiceReferenceImpl
does not implement the requested interface org.osgi.resource.Capability
at org.apache.felix.connect.felix.framework.capabilityset.CapabilitySet.addCapability(CapabilitySet.java:63)
at org.apache.felix.connect.felix.framework.ServiceRegistry.registerService(ServiceRegistry.java:124)
In normal camel-test it's working fine, but in camel-blueprint test I'm getting the exception above. Any help in overcoming this is greatly appreciated.
I ran into the same problem when testing my route with camel blueprint test support. As Claus suggested in the comment, the error went away and the simple test passed after i switched from osgi core version 4.3.1 to 5.0.0:
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
I'm pretty sure that's because the Capability Interface was introduced in osgi release 5:
https://osgi.org/javadoc/r5/core/org/osgi/resource/Capability.html
Btw, I'm also running camel 2.17 and have an almost identical test class as yours.
I have the following scenario:
I have an OSGI bundle that has a service reference defined in the blueprint XML that references an interface in a remote bundle, and a bean that uses one of the impl's methods to populate a Properties object.
Relevant snippet from Bundle #1's XML (the consumer):
...
<!-- other bean definitions, namespace stuff, etc -->
<!-- reference to the fetching service -->
<reference id="fetchingService" interface="company.path.to.fetching.bundle.FetchingService" />
<!-- bean to hold the actual Properties object: the getConfigProperties method is one of the overridden interface methods -->
<bean id="fetchedProperties" class="java.util.Properties" factory-ref="fetchingService" factory-method="getProperties" />
<camelContext id="contextThatNeedsProperties" xmlns="http://camel.apache.org/schema/blueprint">
<propertyPlaceholder id="properties" location="ref:fetchedProperties" />
...
<!-- the rest of the context stuff - routes and so on -->
</camelContext>
Remote Bundle's blueprint.xml:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<cm:property-placeholder id="config-properties" persistent-id="company.path.configfetcher" />
<bean id="fetchingService" class="company.path.to.fetching.bundle.impl.FetchingServiceImpl" scope="singleton" init-method="createLoader" depends-on="config-properties">
<property name="environment" value="${environment}" />
<property name="pathToRestService" value="${restPath}" />
</bean>
<service ref="fetchingService" interface="company.path.to.fetching.bundle.FetchingService" />
<!-- END TESTING -->
</blueprint>
From Impl Class:
public synchronized Properties getProperties() {
if(!IS_RUNNING) {
// timer task that regularly calls the REST api to check for updates
timer.schedule(updateTimerTask, 0, pollInterval);
IS_RUNNING = true;
}
//Map<String, Properties> to return matching object if it's there
if(PROPERTIES_BY_KEY.containsKey(environment)) {
return PROPERTIES_BY_KEY.get(environment);
}
/* if nothing, return an empty Properties object - if this is the case, then whatever bundle is relying on these
* properties is going to fail and we'll see it in the logs
*/
return new Properties();
}
The issue:
I have a test class (extending CamelBlueprintTestSupport) and there are a lot of moving parts such that I can't really change the order of things. Unfortunately, the properties bean method gets called before the CamelContext is started. Not that big a deal because in the test environment there is no config file to read the necessary properties from so the retrieval fails and we get back an empty properties object [note: we're overriding the properties component with fakes since it's not that class being tested], but in a perfect world, I'd like to be able to do two things:
1) replace the service with a new Impl()
2) intercept calls to the getProperties method OR tie the bean to the new service so that the calls return the properties from the fake impl
Thoughts?
Edit #1:
Here's one of the things I'm doing as a workaround right now:
try {
ServiceReference sr = this.getBundleContext().getServiceReference(FetchingService.class);
if(sr != null) {
((FetchingServiceImpl)this.getBundleContext().getService(sr)).setEnvironment(env);
((FetchingServiceImpl)this.getBundleContext().getService(sr)).setPath(path);
}
} catch(Exception e) {
log.error("Error getting Fetching service: {}", e.getMessage());
}
The biggest problem here is that I have to wait until the createCamelContext is called for a BundleContext to exist; therefore, the getProperties call has already happened once. As I said, since in the testing environment no config for the FetchingService class exists to provide the environment and path strings, that first call will fail (resulting in an empty Properties object). The second time around, the code above has set the properties in the impl class and we're off to the races. This is not a question about something that isn't working. Rather, it is about a better, more elegant solution that can be applied in other scenarios.
Oh, and for clarification before anyone asks, the point of this service is so that we don't have to have a .cfg file for every OSGI bundle deployed to our Servicemix instance - this central service will go and fetch the configs that the other bundles need and the only .cfg file that need exist is for the Fetcher.
Other pertinent details:
Camel 2.13.2 - wish it was 2.14 because they've added more property-placeholder tools to that version that would probably make this easier
Servicemix - 5.3.1
Have you tried overriding CamelBlueprintTestSupport's addServicesOnStartup in your test (see "Adding services on startup" http://camel.apache.org/blueprint-testing.html)?
In your case something like:
#Override
protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {
services.put(FetchingService.class.getName(), asService(new FetchServiceImpl(), null));
}
I've enabled my spring application to use transactions and annotated my service method accordingly but the changes to my DB persist when a RuntimeException is thrown.
My Spring configuration looks like this:
<!-- Data Source. -->
<jee:jndi-lookup id="dataSource" jndi-name="java:/jdbc/BeheermoduleDS"/>
<!-- JPA Entity Manager. -->
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/jpa/BeheermoduleDS"/>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
My datasource configuration in my jboss' configuration file looks like this:
<datasource jta="true" jndi-name="java:/jdbc/BeheermoduleDS" pool-name="BeheermoduleDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:sqlserver://localhost:1433;databaseName=Gebruikers;</connection-url>
<driver>sqljdbc</driver>
<security>
<user-name>jboss</user-name>
<password>*****</password>
</security>
</datasource>
My Service method looks like this:
#Transactional
public void authorise(Gebruiker user) {
user.setStatus(GebruikerStatus.Actief.name());
gebruikerRepo.save(user);
if (true) {
throw new RuntimeException("Exception happened just like that");
}
// does more stuff here that is never reached
}
My repository extends a spring data repository and looks like this:
public interface GebruikerRepository extends PagingAndSortingRepository<Gebruiker, Long>, QueryDslPredicateExecutor<Gebruiker> {
}
The transaction is thrown and caught by a controller which just shows a message to the user that an exception occurred. When I check my SQL Server DB, the change made to the user status have been commited.
Weren't they supposed to have been rolled back with the RuntimeException?
After turning debug on for org.springframework.transaction.interceptor I saw that no transactions are being started for my service method, but they are for a bunch of JpaRepository methods.
Also, this is how my persistence.xml looks like:
<persistence-unit name="BeheermodulePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/jdbc/BeheermoduleDS</non-jta-data-source>
Judging from the symptoms you describe you are scanning for the same classes twice. You probably have the same <context:component-scan /> in both the configuration of the ContextLoaderListener and DispatcherServlet.
You want the ContextLoaderListener to scan for everything but #Controller and the DispatcherServlet only for #Controllers. Leading to something like this.
For the ContextLoaderListener
<!-- Load everything except #Controllers -->
<context:component-scan base-package="com.myapp">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
For the DispatcherServlet
<!-- Load everything except #Controllers -->
<context:component-scan base-package="com.myapp" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
See also #Service are constructed twice for another sample and broader explanation.
I have a WSDL file with defines a java.io.Exception:
<xsd:schema xmlns:tns="http://io.java" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://io.java">
<xsd:complexType name="IOException">
<xsd:sequence/>
</xsd:complexType>
</xsd:schema>
When generating Java classes using the Apache CXf wsdl2java task, it generates a class like this (which causes compile errors, as it is not a valid java.io.IoException):
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "IOException")
public class IOException {
}
Is there a way to prevent CXF from generating JDK classes?
Thx! :)
You definitely need to change your namespace.
targetNamespace="http://io.java"
xmlns:tns="http://io.java"
If you have such namespace and the complex type named IOException of course there will a problem. And why in the world you named the namespace like this http://io.java?
Change the namespace for e.g.:
targetNamespace="http://yourcompany.com/yourservice"
xmlns:tns="http://yourcompany.com/yourservice"
You you'll be good.