You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/04/19 13:12:59 UTC
svn commit: r530379 - in /activemq/camel/trunk:
camel-jpa/src/test/java/org/apache/camel/processor/jpa/
camel-spring/src/main/java/org/apache/camel/spring/
Author: jstrachan
Date: Thu Apr 19 04:12:58 2007
New Revision: 530379
URL: http://svn.apache.org/viewvc?view=rev&rev=530379
Log:
added a helper builder to make it a little easier to wire up spring managed resources into a route; such as using a transaction template for specific routes, or using a JpaTemplate etc
Added:
activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java (with props)
Modified:
activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaIdempotentConsumerTest.java
activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringTransactionInterceptor.java
Modified: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaIdempotentConsumerTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaIdempotentConsumerTest.java?view=diff&rev=530379&r1=530378&r2=530379
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaIdempotentConsumerTest.java (original)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaIdempotentConsumerTest.java Thu Apr 19 04:12:58 2007
@@ -21,6 +21,7 @@
import org.apache.camel.Processor;
import org.apache.camel.CamelContext;
import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.spring.SpringRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.IdempotentConsumerTest;
import static org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository.jpaMessageIdRepository;
@@ -42,12 +43,15 @@
@Override
protected RouteBuilder createRouteBuilder(final String endpointUri, final Processor<Exchange> processor) {
- final JpaTemplate jpaTemplate = (JpaTemplate) applicationContext.getBean("jpaTemplate");
-
- return new RouteBuilder() {
+ // START SNIPPET: idempotent
+ return new SpringRouteBuilder<Exchange>() {
public void configure() {
- from(endpointUri).idempotentConsumer(header("messageId"), jpaMessageIdRepository(jpaTemplate, "myProcessorName")).process(processor);
+ from(endpointUri).idempotentConsumer(
+ header("messageId"),
+ jpaMessageIdRepository(bean(JpaTemplate.class), "myProcessorName")
+ ).process(processor);
}
};
+ // END SNIPPET: idempotent
}
}
Added: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java?view=auto&rev=530379
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java (added)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java Thu Apr 19 04:12:58 2007
@@ -0,0 +1,110 @@
+/**
+ *
+ * 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.spring;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.context.ApplicationContext;
+import org.springframework.transaction.support.TransactionTemplate;
+
+/**
+ * An extension of the {@link RouteBuilder} to provide some additional helper methods
+ *
+ * @version $Revision: 1.1 $
+ */
+public abstract class SpringRouteBuilder<E extends Exchange> extends RouteBuilder<E> {
+ private ApplicationContext applicationContext;
+
+ /**
+ * Configures a transaction interceptor on routes created by this builder using the named spring bean
+ * for the {@link TransactionTemplate} to use for the transaction
+ *
+ * @param transactionTemplateName the name of the spring bean in the application context which is the
+ * {@link TransactionTemplate} to use
+ * @return this builder
+ */
+ public SpringRouteBuilder<E> transactionInterceptor(String transactionTemplateName) {
+ TransactionTemplate template = bean(TransactionTemplate.class, transactionTemplateName);
+ setTransactionInterceptor(new SpringTransactionInterceptor(template));
+ return this;
+ }
+
+ /**
+ * Looks up the bean with the given name in the application context and returns it, or throws an exception if the
+ * bean is not present or is not of the given type
+ *
+ * @param type the type of the bean
+ * @param beanName the name of the bean in the application context
+ * @return the bean
+ */
+ public <T> T bean(Class<T> type, String beanName) {
+ ApplicationContext context = getApplicationContext();
+ return (T) context.getBean(beanName, type);
+ }
+
+ /**
+ * Looks up the bean with the given type in the application context and returns it, or throws an exception if the
+ * bean is not present or there are multiple possible beans to choose from for the given type
+ *
+ * @param type the type of the bean
+ * @return the bean
+ */
+ public <T> T bean(Class<T> type) {
+ ApplicationContext context = getApplicationContext();
+ String[] names = context.getBeanNamesForType(type, true, true);
+ if (names != null) {
+ int count = names.length;
+ if (count == 1) {
+ // lets instantiate the single bean
+ return (T) context.getBean(names[0]);
+ }
+ else if (count > 1) {
+ throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
+ }
+ }
+ throw new IllegalArgumentException("No bean available in the application context of type: " + type);
+ }
+
+ /**
+ * Returns the application context which has been configured via the {@link #setApplicationContext(ApplicationContext)}
+ * method or from the underlying {@link SpringCamelContext}
+ *
+ * @return
+ */
+ public ApplicationContext getApplicationContext() {
+ if (applicationContext == null) {
+ CamelContext camelContext = getContext();
+ if (camelContext instanceof SpringCamelContext) {
+ SpringCamelContext springCamelContext = (SpringCamelContext) camelContext;
+ return springCamelContext.getApplicationContext();
+ }
+ else {
+ throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
+ }
+ }
+ return applicationContext;
+ }
+
+ /**
+ * Sets the application context to use to lookup beans
+ */
+ public void setApplicationContext(ApplicationContext applicationContext) {
+ this.applicationContext = applicationContext;
+ }
+}
Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringRouteBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringTransactionInterceptor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringTransactionInterceptor.java?view=diff&rev=530379&r1=530378&r2=530379
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringTransactionInterceptor.java (original)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringTransactionInterceptor.java Thu Apr 19 04:12:58 2007
@@ -35,6 +35,13 @@
private TransactionTemplate template;
+ public SpringTransactionInterceptor() {
+ }
+
+ public SpringTransactionInterceptor(TransactionTemplate template) {
+ this.template = template;
+ }
+
public Processor<E> addIntercetors(final Processor<E> processor) {
final TransactionTemplate transactionTemplate = getTemplate();
if (transactionTemplate == null) {