1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.jpa; |
18 |
|
|
19 |
|
import java.util.Iterator; |
20 |
|
|
21 |
|
import javax.persistence.EntityManager; |
22 |
|
import javax.persistence.PersistenceException; |
23 |
|
|
24 |
|
import org.apache.camel.Exchange; |
25 |
|
import org.apache.camel.Expression; |
26 |
|
import org.apache.camel.converter.ObjectConverter; |
27 |
|
import org.apache.camel.impl.DefaultProducer; |
28 |
|
|
29 |
|
import org.springframework.orm.jpa.JpaCallback; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public class JpaProducer extends DefaultProducer<Exchange> { |
35 |
|
private final TransactionStrategy template; |
36 |
|
private final Expression<Exchange> expression; |
37 |
|
|
38 |
|
public JpaProducer(JpaEndpoint endpoint, Expression<Exchange> expression) { |
39 |
3 |
super(endpoint); |
40 |
3 |
this.expression = expression; |
41 |
3 |
this.template = endpoint.createTransactionStrategy(); |
42 |
3 |
} |
43 |
|
|
44 |
|
public void process(Exchange exchange) { |
45 |
3 |
final Object values = expression.evaluate(exchange); |
46 |
3 |
if (values != null) { |
47 |
3 |
template.execute(new JpaCallback() { |
48 |
3 |
public Object doInJpa(EntityManager entityManager) throws PersistenceException { |
49 |
3 |
Iterator iter = ObjectConverter.iterator(values); |
50 |
6 |
while (iter.hasNext()) { |
51 |
3 |
Object value = iter.next(); |
52 |
3 |
entityManager.persist(value); |
53 |
3 |
} |
54 |
3 |
return null; |
55 |
|
} |
56 |
|
}); |
57 |
|
} |
58 |
3 |
} |
59 |
|
} |