Versions Compared

Key

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

...

Code Block
java
java
/*
 * (non-Javadoc)
 * 
 * @see java.util.TimerTask#run()
 */
public void run() {
	try {
        InOnly inOnly = serviceContext.createInOnly(new QName(
				"http://tempuri.org/logger", "log"));
		NormalizedMessage message = inOnly.createMessage();
		message.setContent(new StreamSource(new StringReader(
				"<hello>world</hello>")));
		serviceContext.done(inOnly);
	} catch (MessagingException e) {
		e.printStackTrace();
	}
}

Thats That's it for building your first component's code. Now, you need to do the wiring for the JBI, i.e., you will need to make sure that when you publish to the interface, that it maps to a specific service name. Since you are still writing the first component, you will have to make up the name of the second component as a placeholder. However, keep this name in mind since you will be writing a second component to receive these messages shortly. To do the JBI wiring, look to the /src/main/merge/services.xml file. You should be able to prepare the file as shown below:

...

NOTE: The interface name is described as a consumes, which in JBI-speak means that this component will be sending its exchange to the servicename 'logger:myLogger'. You haven't written this service yet, but as you can see, it won't take you long to create this service so that you can use its services.xml to expose the service-name and interface name. The SSCT lets you work through the interface name, though you still have the ability to publish to any of the available endpoints EndPoints that are defined in the services.xml.

Next, you need to tell the Spring Component that an instance of the TimerComponent is required, this . This is done in the Spring configuration file found in /src/main/jbi/META-INF/jbi-spring.xml, as can be seen below:

...

Congratulations! You have just created your first JBI component!

Creating a

...

Component to

...

Receive the

...

Exchange

Of course, creating single JBI The one problem with JBI is that a single component is pretty useless, much like being the only person at the party there is very little to do. So we can , the next step is for you to return to the parent directory and repeat the process above, using our Maven JBI plugin and the maven jbi:createArchetype, to create a second component called aptely , which you will call mySecondComponent.

Once you have created the project, and also the eclipse Eclipse files, you can create a new class in the package org.servicemix.tutorial called LoggerComponent, this . This time we you will implement be implementing the ServiceInterfaceImplementation interface, and we you will fill in the getInterface method with the name of the interface we you used in building the first component., as shown below:

Code Block
java
java
package org.servicemix;

import javax.jbi.messaging.MessageExchange;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.servicemix.client.ServiceContext;
import org.servicemix.client.ServiceInterfaceImplementation;

/**
 * A simple Logger based component
 * 
 * @author <a href="mailto:pdodds@unity-systems.com">Philip Dodds </a>
 */
public class LoggerComponent implements ServiceInterfaceImplementation {
	
	private static final Log log = LogFactory.getLog(LoggerComponent.class
			.getName());

	/* (non-Javadoc)
	 * @see org.servicemix.client.ServiceInterfaceImplementation#getInterfaceName()
	 */
	public QName getInterfaceName() {
		return new QName(
				"http://tempuri.org/timer", "notification");
	}

	/* (non-Javadoc)
	 * @see org.servicemix.client.ServiceImplementation#setServiceContext(org.servicemix.client.ServiceContext)
	 */
	public void setServiceContext(ServiceContext arg0) {
		
	}

	/* (non-Javadoc)
	 * @see org.servicemix.client.ServiceImplementation#onMessage(javax.jbi.messaging.MessageExchange)
	 */
	public void onMessage(MessageExchange exchange) {
		log.info("I got my exchange "+exchange);
	}

}

The Spring Client Toolkit will make sure SSCT will ensure that exchanges for this service and that interface its interfaces will be passed to this the ServiceInterfaceImplementation.

Next we , you will need to set up the services.xml for this component, since . Since this is such a simple example we are able to , you can simply reverse the logic from the first component, so that this this component provides the interface on for itself.

Code Block
xml
xml
<!-- This is where we are able to declare the ins and outs of the Service
 	  as it consumes and provides elements,  the service-names and endpointsEndPoints
 	  will be used to activate the JBI endpointsEndPoints,  and weyou can use the 
 	  interface-name to determine which one weyou want to call and also which
 	  bean is going to receive the actual JBI exchange -->
<services binding-component="false"
	xmlns:timer="http://tempuri.org/timer"
	xmlns:logger="http://tempuri.org/logger">
	<provides interface-name="timer:notification"
		service-name="logger:write" />
</services>

And once Once again we you need to configure the spring Spring container to create an instance of our your bean in the src/main/jbi/META-INF/jbi-spring.xml file, shown below:

Code Block
xml
xml
<beans>
	<!--  This is going to be where we declare the beans that we want the
		   SpringComponent container to build -->
	<bean id="myLogger"
		class="org.servicemix.tutorial.LoggerComponent">		
	</bean> 
</beans>

And that This now completes the writing of a recieving component, and as the receiving component's code. As with the first component we , you need to build this component the receiving component so that it is ready for deployment using the maven jbi:generateInstaller goal to onto a JBI server (in our case the wonderful ServiceMix).

Deploying

...

Your Components onto ServiceMix

Once of the key differences between the Spring Client Toolkit and the standard ServiceMix client libraries is that the Spring Client Toolkit is designed to generate standard JBI components and service units that can run in any JBI compliant container, this means that you are able to build installable zip files that can be deployed into the server, in this example we are going to deploy to a stand-alone servicemix server, however you could also deploy to a Geronimo and JBoss instance running servicemix embedded.

...