Skip to content

Commit

Permalink
CAMEL-2228 Fixed the issue that camel-example-spring-javaconfig can n…
Browse files Browse the repository at this point in the history
…ot work in Servicemix

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@884462 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
WillemJiang committed Nov 26, 2009
1 parent 8304f77 commit ad4e77f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}

protected DefaultCamelContext newCamelContext() {
return new DefaultCamelContext();
}

public DefaultCamelContext createContext() {
LOG.debug("Creating DefaultCamelContext");

DefaultCamelContext context = new DefaultCamelContext();
DefaultCamelContext context = newCamelContext();
if (bundleContext != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using OSGI resolvers");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.osgi;

import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringCamelContextFactory extends CamelContextFactory implements ApplicationContextAware {
private ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext context) {
this.applicationContext = context;
}

@Override
protected DefaultCamelContext newCamelContext() {
return new SpringCamelContext(applicationContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CamelBeanPostProcessor camelBeanPostProcessor() throws Exception {
*/
@Bean
public CamelContext camelContext() throws Exception {
SpringCamelContext camelContext = new SpringCamelContext(getApplicationContext());
CamelContext camelContext = createCamelContext();
setupCamelContext(camelContext);
List<RouteBuilder> routes = routes();
for (RoutesBuilder route : routes) {
Expand All @@ -61,8 +61,13 @@ public CamelContext camelContext() throws Exception {
return camelContext;
}

// maybe register the camel component, language here
public void setupCamelContext(CamelContext camelContext) throws Exception {
// Can register the camel component, language here
protected void setupCamelContext(CamelContext camelContext) throws Exception {

}

protected CamelContext createCamelContext() throws Exception {
return new SpringCamelContext(getApplicationContext());
}

/**
Expand Down
17 changes: 15 additions & 2 deletions examples/camel-example-spring-javaconfig/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
<description>An example showing how to work with Camel and Spring Java Config</description>

<properties>
<camel.osgi.export.pkg>org.apache.camel.example.spring.*</camel.osgi.export.pkg>
<camel.osgi.import.pkg>org.apache.activemq.xbean,org.apache.activemq.broker,org.apache.activemq.pool,*</camel.osgi.import.pkg>
<camel.osgi.export>org.apache.camel.example.spring.*</camel.osgi.export>
<camel.osgi.import>org.apache.activemq.xbean,org.apache.activemq.broker,org.apache.activemq.pool</camel.osgi.import>
<!-- avoid to import the bunch of spring related package -->
<camel.osgi.dynamic>*</camel.osgi.dynamic>
</properties>

<dependencies>
Expand All @@ -46,6 +48,11 @@
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-osgi</artifactId>
</dependency>

<dependency>
Expand All @@ -57,6 +64,12 @@
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
*/
package org.apache.camel.example.spring.javaconfig;

import java.util.List;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;

import org.apache.camel.osgi.SpringCamelContextFactory;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.camel.spring.javaconfig.Main;
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
import org.osgi.framework.BundleContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.config.java.annotation.Bean;
import org.springframework.config.java.annotation.Configuration;
import org.springframework.osgi.context.BundleContextAware;

//START SNIPPET: RouteConfig
/**
Expand All @@ -32,7 +41,10 @@
* @version $Revision$
*/
@Configuration
public class MyRouteConfig extends SingleRouteCamelConfiguration {
public class MyRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean, BundleContextAware {

private BundleContext bundleContext;

/**
* Allow this route to be run as an application
*
Expand All @@ -43,19 +55,38 @@ public static void main(String[] args) throws Exception {
new Main().run(args);
}

public BundleContext getBundleContext() {
return bundleContext;
}

public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}


/**
* Returns the CamelContext which support OSGi
*/
@Override
protected CamelContext createCamelContext() throws Exception {
SpringCamelContextFactory factory = new SpringCamelContextFactory();
factory.setApplicationContext(getApplicationContext());
factory.setBundleContext(getBundleContext());
return factory.createContext();
}

@Override
// setup the ActiveMQ component and register it into the camel context
public void setupCamelContext(CamelContext camelContext) throws Exception {
protected void setupCamelContext(CamelContext camelContext) throws Exception {
JmsComponent answer = new JmsComponent();
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("vm://localhost?broker.persistent=false&broker.useJmx=false");
connectionFactory.setBrokerURL("vm://localhost.spring.javaconfig?marshal=false&broker.persistent=false&broker.useJmx=false");
answer.setConnectionFactory(connectionFactory);
camelContext.addComponent("jms", answer);
}


public static class SomeBean {

public void someMethod(String body) {
System.out.println("Received: " + body);
}
Expand All @@ -80,5 +111,9 @@ public void configure() {
}
};
}

public void afterPropertiesSet() throws Exception {
// just to make SpringDM happy do nothing here
}
}
//END SNIPPET: RouteConfig

0 comments on commit ad4e77f

Please sign in to comment.