You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by szplayer <sz...@hotmail.com> on 2011/08/18 12:24:34 UTC

Transaction configuration in Java DSL

I'm trying to moving data from local folder(file) to ActiveMQ with calling a
process, and ensuring no data lost during network failure or exception throw
by the process. So TRANSACTION comes into my mind. But I can't get it run in
Java DSL. (Spring DSL will be okay strangely.) 

public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory mqFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
		
		PooledConnectionFactory mqConnPool = new PooledConnectionFactory();
		mqConnPool.setConnectionFactory(mqFactory);
		mqConnPool.setMaxConnections(5);		
				
		JmsTransactionManager txnManager = new JmsTransactionManager();
		txnManager.setConnectionFactory(mqConnPool);
		
		ActiveMQComponent mqComponent = new ActiveMQComponent();
		mqComponent.setConnectionFactory(mqConnPool);
		mqComponent.setTransactionManager(txnManager);
		mqComponent.setTransacted(true);
		mqComponent.setUsePooledConnection(true);
		
		CamelContext context;
		context = new DefaultCamelContext();
		context.addComponent("activemq",mqComponent);
		context.addRoutes(new MyRouteBuilder());
		
		context.start();
		
		while (true)
		{
			
		}
}

private static class MyRouteBuilder extends RouteBuilder  {
		@Override
        public void configure() throws Exception {   
			from("file:c:\\temp\\inbox")
				.transacted()
				.to("activemq:test")
				.process(new Processor() {
					public void process(Exchange exchange) throws Exception { 
						exchange.setException(new Exception("abc"));
					}
				});
		}
	}

when run the problem, it throws 
Exception in thread "main" org.apache.camel.FailedToCreateRouteException:
Failed to create route route1 at: >>> Policy[ref:null] <<<

from previous post, i found that .transacted() is only work for Spring
Transaction.

is there any other solution to make it "transacted" ??




--
View this message in context: http://camel.465427.n5.nabble.com/Transaction-configuration-in-Java-DSL-tp4711625p4711625.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Transaction configuration in Java DSL

Posted by Claus Ibsen <cl...@gmail.com>.
The transacted() will lookup in the Camel registry for a
TransactionMananger instance and hook into using that.
When using Spring XML files then that by declaring a spring <bean> tag
then its enlisted in the spring app context, which is where Camel will
look as well.

So if you do it manually from java code, you can enlist the
JmsTransactionManager yourself.

Just create a SimpleRegistry and add the TX manager to this registry.
And then pass that registry to the DefaultCamelContext ctr.


However if you only use JMS and you have already configured it to be
transactional, then you may omit using .transcated() in the Camel
route.
However if you use more resources such as JDBC etc. then you would
need to use the .transacted().

Also by using .transacted() you can see in the logs when a new TX is
begin / commit / rollback events.




On Thu, Aug 18, 2011 at 12:24 PM, szplayer <sz...@hotmail.com> wrote:
> I'm trying to moving data from local folder(file) to ActiveMQ with calling a
> process, and ensuring no data lost during network failure or exception throw
> by the process. So TRANSACTION comes into my mind. But I can't get it run in
> Java DSL. (Spring DSL will be okay strangely.)
>
> public static void main(String[] args) throws Exception {
> ActiveMQConnectionFactory mqFactory = new
> ActiveMQConnectionFactory("tcp://localhost:61616");
>
>                PooledConnectionFactory mqConnPool = new PooledConnectionFactory();
>                mqConnPool.setConnectionFactory(mqFactory);
>                mqConnPool.setMaxConnections(5);
>
>                JmsTransactionManager txnManager = new JmsTransactionManager();
>                txnManager.setConnectionFactory(mqConnPool);
>
>                ActiveMQComponent mqComponent = new ActiveMQComponent();
>                mqComponent.setConnectionFactory(mqConnPool);
>                mqComponent.setTransactionManager(txnManager);
>                mqComponent.setTransacted(true);
>                mqComponent.setUsePooledConnection(true);
>
>                CamelContext context;
>                context = new DefaultCamelContext();
>                context.addComponent("activemq",mqComponent);
>                context.addRoutes(new MyRouteBuilder());
>
>                context.start();
>
>                while (true)
>                {
>
>                }
> }
>
> private static class MyRouteBuilder extends RouteBuilder  {
>                @Override
>        public void configure() throws Exception {
>                        from("file:c:\\temp\\inbox")
>                                .transacted()
>                                .to("activemq:test")
>                                .process(new Processor() {
>                                        public void process(Exchange exchange) throws Exception {
>                                                exchange.setException(new Exception("abc"));
>                                        }
>                                });
>                }
>        }
>
> when run the problem, it throws
> Exception in thread "main" org.apache.camel.FailedToCreateRouteException:
> Failed to create route route1 at: >>> Policy[ref:null] <<<
>
> from previous post, i found that .transacted() is only work for Spring
> Transaction.
>
> is there any other solution to make it "transacted" ??
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Transaction-configuration-in-Java-DSL-tp4711625p4711625.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Transaction configuration in Java DSL

Posted by szplayer <sz...@hotmail.com>.
since the processor always throws exception. in this case i don't want the
message send to the MQ.

Thanks~

--
View this message in context: http://camel.465427.n5.nabble.com/Transaction-configuration-in-Java-DSL-tp4711625p4711650.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Transaction configuration in Java DSL

Posted by szplayer <sz...@hotmail.com>.
It's works !!

Thank you so much

--
View this message in context: http://camel.465427.n5.nabble.com/Transaction-configuration-in-Java-DSL-tp4711625p4714220.html
Sent from the Camel - Users mailing list archive at Nabble.com.