Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

So now, it is time to create the interface that we will use through this project. Open Eclipse environment if not already done and create a new folder "service" under src/main/java/demo tree. Add the interface 'TransformService.java' and copy paste the code hereunder :

Code Block
java
java
titleTransformService.java
package demo.service;

public interface TransformService {|

	public Object transform(Object obj);

}

...

Create the class 'TransformServiceImpl.java' under the folder src/main/java/demo/service

Code Block
java
java
titleTransformServiceImpl.java
package demo.service;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @version $Revision: 640450 $
 */
public class TransformServiceImpl implements TransformService {
    private static final transient Log LOG = LogFactory.getLog(TransformServiceImpl.class);
    private boolean verbose = true;
    private String prefix = "MyTransform";

    public Object transform(Object body) {
        String answer = prefix + " set body:  " + new Date();
        if (verbose) {
            System.out.println(">> call >> " + answer);
        }
        LOG.info(">> call >>" + answer);
        return answer;
    }

    public boolean isVerbose() {
        return verbose;
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}

...

Create the file 'demo-service-bundle-contxt.xml' under the folder 'src/main/resources/META-INF/spring'

Code Block
xml
xml
titledemo-service-bundle-contxt.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- regular spring configuration file defining the beans for this
       bundle. The configuration of OSGi definitions is kept in a separate
       configuration file so that this file can easily be used
       for integration testing outside of an OSGi environment -->
    <bean id="transformService"
	      class="demo.service.TransformServiceImpl">
	</bean>
</beans>

...

Create the file 'demo-service-bundle-contxt-osgi.xml' under the folder 'src/main/resources/META-INF/spring'

Code Block
xml
xml
titledemo-service-bundle-contxt-osgi.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:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<!--
		definitions using elements of the osgi namespace can be included in
		this file. There is no requirement to keep these definitions in a
		separate file if you do not want to. The rationale for keeping these
		definitions separate is to facilitate integration testing of the
		bundle outside of an OSGi container
	-->

	<osgi:service ref="transformService">
		<osgi:interfaces>
			<value>demo.service.TransformService</value>
		</osgi:interfaces>
	</osgi:service>

</beans>

...

  • For the routing, create the following file under the folder 'src/main/resources/META-INF/spring" of the project 'demo.camel-bundle'
Code Block
xml
xml
titleCamelContext.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://activemq.apache.org/camel/schema/spring
       http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
    <route>
      <from uri="timer://myTimer?fixedRate=true&amp;period=10000"/>
      <bean ref="myTransform" method="transform"/>
      <to uri="log:ExampleRouter"/>
    </route>
  </camelContext>
</beans>

...

  • To inject the dependency, we will create a second file named 'bundle-context-osgi.xml' in the same folder
Code Block
xml
xml
titlebundle-context-osgi.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:osgi="http://www.springframework.org/schema/osgi"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/osgi
  http://www.springframework.org/schema/osgi/spring-osgi.xsd">

      <osgi:reference id="myTransform" interface="demo.service.TransformService"/>

</beans>

...