You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Jeff Gunther <Je...@intalgent.com> on 2007/05/10 07:46:46 UTC

[camel] Spring and Custom Components

Hi All,

Here is the code snippet I'm working to configure in Spring:

final CamelContext container = new DefaultCamelContext();
container.addComponent("foo", new FooComponent(container));

I'm using the Spring 2.0 XML Namespaces approach to define my routes.  
How can I add a custom component to a "camelContext"?

Jeff Gunther

Re: [camel] Spring and Custom Components

Posted by James Strachan <ja...@gmail.com>.
On 5/11/07, Jeff Gunther <je...@intalgent.com> wrote:
> On May 11, 2007, at 11:16 PM, James Strachan wrote:
>
> > On 5/10/07, Jeff Gunther <je...@intalgent.com> wrote:
> >> Hi James,
> >>
> >> Thank you for the very detailed follow up! That worked wonderfully.
> >
> > Great! Just out of interest, do you fancy contributing your
> > component :)
>
> Sure. What's the best method to send you the component?

Great! :)

There's a general document on contributing here...
http://activemq.apache.org/camel/contributing.html

but basically, just raise a JIRA and attach whatever code/patches you
like and we can take it from there.


> >> Related to this issue, how would I set the ConnectionFactory for the
> >> JMS Component?
> >
> > Here's an example that shows you how to do it in Java code...
> > http://activemq.apache.org/camel/walk-through-an-example.html
> >
> > Also I've just updated the wiki to show how to do the same thing via
> > Spring XML also (you might need to hit refresh on your browser)
> > http://cwiki.apache.org/CAMEL/how-do-i-add-a-component.html
> > http://cwiki.apache.org/CAMEL/how-do-i-configure-endpoints.html
>
>  From my testing, it appears that the modification you made to the
> JmsComponent starts it's own broker. Is this accurate?

Yes - the broker is started if you use the brokerURL of vm://localhost...


> If so, how can
>  I connect to a standalone broker

If you prefer to connect to a remote broker then use tcp://host:port
instead. To handle auto-reconnection use failover:tcp://host:port


> or how can I configure the
> JmsComponent to use an ActiveMQ persistence adapter and MySQL data-
> source?

You could explicitly configure the broker inside the spring.xml as
well - though you do have to be a little careful that the broker is
started first before the ConnectionFactory (so usually you need to use
the spring xml dependsOn stuff) - as if you are not careful the vm://
transport will auto-create a broker for you first


> Currently, I'm using the following Spring configuration to use a
> ActiveMQ persistence adapter and a MySQL data-source:
>
>         <bean id="broker" class="org.apache.activemq.broker.BrokerService"
> init-method="start" destroy-method="stop">
>                 <property name="brokerName" value = "broker"/>
>                 <property name="persistent" value="true"/>
>                 <property name="useJmx" value = "false"/>
>                 <property name="transportConnectorURIs">
>                         <list>
>                                 <value>vm://localhost</value>
>                                 <value>tcp://localhost:9000</value>
>                         </list>
>                 </property>
>                 <property name="persistenceAdapter" ref="persistenceAdapter"/>
>         </bean>
>
>         <bean id="persistenceAdapter"
> class="org.apache.activemq.store.PersistenceAdapterFactoryBean">
>             <property name="dataSource" ref="mysql-ds"/>
>             <property name="dataDirectory" value="activemq-data"/>
>             <property name="journalLogFiles" value="5"/>
>         </bean>
>
>         <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource"
> destroy-method="close">
>             <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
>             <property name="url" value="jdbc:mysql://localhost/activemq?
> relaxAutoCommit=true"/>
>             <property name="username" value="root"/>
>             <property name="password" value=""/>
>             <property name="poolPreparedStatements" value="true"/>
>         </bean>

Looks good to me. You could try the Spring 2 XML stuff if you want -
but the above looks fine.

If you prefer to run the broker separately, you could just edit the
activemq.xml that ships with ActiveMQ to configure the MySQL stuff and
use the tcp:// transport to connect to it

-- 
James
-------
http://macstrac.blogspot.com/

Re: [camel] Spring and Custom Components

Posted by Jeff Gunther <je...@intalgent.com>.
On May 11, 2007, at 11:16 PM, James Strachan wrote:

> On 5/10/07, Jeff Gunther <je...@intalgent.com> wrote:
>> Hi James,
>>
>> Thank you for the very detailed follow up! That worked wonderfully.
>
> Great! Just out of interest, do you fancy contributing your  
> component :)

Sure. What's the best method to send you the component?

>> Related to this issue, how would I set the ConnectionFactory for the
>> JMS Component?
>
> Here's an example that shows you how to do it in Java code...
> http://activemq.apache.org/camel/walk-through-an-example.html
>
> Also I've just updated the wiki to show how to do the same thing via
> Spring XML also (you might need to hit refresh on your browser)
> http://cwiki.apache.org/CAMEL/how-do-i-add-a-component.html
> http://cwiki.apache.org/CAMEL/how-do-i-configure-endpoints.html

 From my testing, it appears that the modification you made to the  
JmsComponent starts it's own broker. Is this accurate? If so, how can  
I connect to a standalone broker or how can I configure the  
JmsComponent to use an ActiveMQ persistence adapter and MySQL data- 
source?

Currently, I'm using the following Spring configuration to use a  
ActiveMQ persistence adapter and a MySQL data-source:

	<bean id="broker" class="org.apache.activemq.broker.BrokerService"  
init-method="start" destroy-method="stop">
		<property name="brokerName" value = "broker"/>
		<property name="persistent" value="true"/>
		<property name="useJmx" value = "false"/>
		<property name="transportConnectorURIs">
			<list>
				<value>vm://localhost</value>
				<value>tcp://localhost:9000</value>
			</list>
		</property>
		<property name="persistenceAdapter" ref="persistenceAdapter"/>
	</bean>

	<bean id="persistenceAdapter"  
class="org.apache.activemq.store.PersistenceAdapterFactoryBean">
	    <property name="dataSource" ref="mysql-ds"/>
	    <property name="dataDirectory" value="activemq-data"/>
	    <property name="journalLogFiles" value="5"/>
   	</bean>

	<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource"  
destroy-method="close">
	    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	    <property name="url" value="jdbc:mysql://localhost/activemq? 
relaxAutoCommit=true"/>
	    <property name="username" value="root"/>
	    <property name="password" value=""/>
	    <property name="poolPreparedStatements" value="true"/>
   	</bean>

Jeff Gunther



Re: [camel] Spring and Custom Components

Posted by James Strachan <ja...@gmail.com>.
On 5/10/07, Jeff Gunther <je...@intalgent.com> wrote:
> Hi James,
>
> Thank you for the very detailed follow up! That worked wonderfully.

Great! Just out of interest, do you fancy contributing your component :)


> Related to this issue, how would I set the ConnectionFactory for the
> JMS Component?

Here's an example that shows you how to do it in Java code...
http://activemq.apache.org/camel/walk-through-an-example.html

Also I've just updated the wiki to show how to do the same thing via
Spring XML also (you might need to hit refresh on your browser)
http://cwiki.apache.org/CAMEL/how-do-i-add-a-component.html
http://cwiki.apache.org/CAMEL/how-do-i-configure-endpoints.html

-- 
James
-------
http://macstrac.blogspot.com/

Re: [camel] Spring and Custom Components

Posted by Jeff Gunther <je...@intalgent.com>.
Hi James,

Thank you for the very detailed follow up! That worked wonderfully.  
Related to this issue, how would I set the ConnectionFactory for the  
JMS Component?

Jeff Gunther

On May 10, 2007, at 11:27 AM, James Strachan wrote:

> Hi Jeff
>
> On 5/9/07, Jeff Gunther <Je...@intalgent.com> wrote:
>> Hi All,
>>
>> Here is the code snippet I'm working to configure in Spring:
>>
>> final CamelContext container = new DefaultCamelContext();
>> container.addComponent("foo", new FooComponent(container));
>
> FWIW if you don't need to explicitly configure the FooComponent
> directly if it doesn't have much configuration data - you can create a
> file called
>
> /META-INF/services/org/apache/camel/component/foo
>
> with contents
>
> class=org.acme.FooComponent
>
> (you can add other property configurations in there too if you like)
>
> Then if you refer to an endpoint as "foo://somethingOrOther" Camel
> will auto-discover your component and register it.
>
> The downside of this approach though is if FooComponent needs some
> actual injection; like a DataSource or whatever. You can configure
> endpoints in a few different ways...
> http://cwiki.apache.org/CAMEL/how-do-i-configure-endpoints.html
>
>
> One thing thats not quite tested or documented yet that I wanted was
> the ability to use the Injector on a CamelContext to dependency inject
> the FooComponent (using either autowire mode in Spring or using
> @Resource type dependency injection) so that you don't have to
> explicitly add the FooComponent to the Spring XML, you just need to
> add the resources which need to be injected into the FooComponent.
> (This also has the nice benefit of avoiding component developers
> having to worry about writing Spring XML namespace handler stuff)
>
>
>> I'm using the Spring 2.0 XML Namespaces approach to define my routes.
>> How can I add a custom component to a "camelContext"?
>
> If after my long ramble above you still want to explicitly have a
> <fooComponent cheese="edam"/> type XML within the <camelContext/> then
> you need to do some Spring XML NamespaceHandler hacking - which is
> kinda ugly & crufty work unfortunately. You could use xbean-spring
> which tries to automate most of this work for you.
>
> If you look in camel-spring at CamelNamespaceHandler you'll see how we
> handle the Spring XML stuff (warning its kinda hairy code to look at).
> If you wanted <fooComponent> to be a standard part of the core Camel
> schema then you'd hack that file to add your component & add a patch
> to the camel XSD. Otherwise you could write your own namespace &
> schema if you prefer.
>
>
> Incidentally I've updated the website with more details
> http://cwiki.apache.org/CAMEL/how-do-i-add-a-component.html
> http://cwiki.apache.org/CAMEL/writing-components.html
> -- 
> James
> -------
> http://macstrac.blogspot.com/


Re: [camel] Spring and Custom Components

Posted by James Strachan <ja...@gmail.com>.
Hi Jeff

On 5/9/07, Jeff Gunther <Je...@intalgent.com> wrote:
> Hi All,
>
> Here is the code snippet I'm working to configure in Spring:
>
> final CamelContext container = new DefaultCamelContext();
> container.addComponent("foo", new FooComponent(container));

FWIW if you don't need to explicitly configure the FooComponent
directly if it doesn't have much configuration data - you can create a
file called

/META-INF/services/org/apache/camel/component/foo

with contents

class=org.acme.FooComponent

(you can add other property configurations in there too if you like)

Then if you refer to an endpoint as "foo://somethingOrOther" Camel
will auto-discover your component and register it.

The downside of this approach though is if FooComponent needs some
actual injection; like a DataSource or whatever. You can configure
endpoints in a few different ways...
http://cwiki.apache.org/CAMEL/how-do-i-configure-endpoints.html


One thing thats not quite tested or documented yet that I wanted was
the ability to use the Injector on a CamelContext to dependency inject
the FooComponent (using either autowire mode in Spring or using
@Resource type dependency injection) so that you don't have to
explicitly add the FooComponent to the Spring XML, you just need to
add the resources which need to be injected into the FooComponent.
(This also has the nice benefit of avoiding component developers
having to worry about writing Spring XML namespace handler stuff)


> I'm using the Spring 2.0 XML Namespaces approach to define my routes.
> How can I add a custom component to a "camelContext"?

If after my long ramble above you still want to explicitly have a
<fooComponent cheese="edam"/> type XML within the <camelContext/> then
you need to do some Spring XML NamespaceHandler hacking - which is
kinda ugly & crufty work unfortunately. You could use xbean-spring
which tries to automate most of this work for you.

If you look in camel-spring at CamelNamespaceHandler you'll see how we
handle the Spring XML stuff (warning its kinda hairy code to look at).
If you wanted <fooComponent> to be a standard part of the core Camel
schema then you'd hack that file to add your component & add a patch
to the camel XSD. Otherwise you could write your own namespace &
schema if you prefer.


Incidentally I've updated the website with more details
http://cwiki.apache.org/CAMEL/how-do-i-add-a-component.html
http://cwiki.apache.org/CAMEL/writing-components.html
-- 
James
-------
http://macstrac.blogspot.com/