1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.processor.idempotent.jpa; |
18 |
|
|
19 |
|
import java.util.List; |
20 |
|
|
21 |
|
import javax.persistence.EntityManagerFactory; |
22 |
|
import javax.persistence.Persistence; |
23 |
|
|
24 |
|
import org.apache.camel.processor.idempotent.MessageIdRepository; |
25 |
|
|
26 |
|
import org.springframework.orm.jpa.JpaTemplate; |
27 |
|
import org.springframework.orm.jpa.JpaTransactionManager; |
28 |
|
import org.springframework.transaction.TransactionDefinition; |
29 |
|
import org.springframework.transaction.TransactionStatus; |
30 |
|
import org.springframework.transaction.support.TransactionCallback; |
31 |
|
import org.springframework.transaction.support.TransactionTemplate; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
12 |
public class JpaMessageIdRepository implements MessageIdRepository { |
37 |
1 |
protected static final String QUERY_STRING = "select x from " + MessageProcessed.class.getName() + " x where x.processorName = ?1 and x.messageId = ?2"; |
38 |
|
private JpaTemplate jpaTemplate; |
39 |
|
private String processorName; |
40 |
|
private TransactionTemplate transactionTemplate; |
41 |
|
|
42 |
|
public JpaMessageIdRepository(JpaTemplate template, String processorName) { |
43 |
1 |
this(template, createTransactionTemplate(template), processorName); |
44 |
1 |
} |
45 |
|
|
46 |
1 |
public JpaMessageIdRepository(JpaTemplate template, TransactionTemplate transactionTemplate, String processorName) { |
47 |
1 |
this.jpaTemplate = template; |
48 |
1 |
this.processorName = processorName; |
49 |
1 |
this.transactionTemplate = transactionTemplate; |
50 |
1 |
} |
51 |
|
|
52 |
|
public static JpaMessageIdRepository jpaMessageIdRepository(String persistenceUnit, String processorName) { |
53 |
0 |
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit); |
54 |
0 |
return jpaMessageIdRepository(new JpaTemplate(entityManagerFactory), processorName); |
55 |
|
} |
56 |
|
|
57 |
|
public static JpaMessageIdRepository jpaMessageIdRepository(JpaTemplate jpaTemplate, String processorName) { |
58 |
1 |
return new JpaMessageIdRepository(jpaTemplate, processorName); |
59 |
|
} |
60 |
|
|
61 |
|
private static TransactionTemplate createTransactionTemplate(JpaTemplate jpaTemplate) { |
62 |
1 |
TransactionTemplate transactionTemplate = new TransactionTemplate(); |
63 |
1 |
transactionTemplate.setTransactionManager(new JpaTransactionManager(jpaTemplate.getEntityManagerFactory())); |
64 |
1 |
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); |
65 |
1 |
return transactionTemplate; |
66 |
|
} |
67 |
|
|
68 |
|
public boolean contains(final String messageId) { |
69 |
|
|
70 |
6 |
Boolean rc = (Boolean)transactionTemplate.execute(new TransactionCallback() { |
71 |
6 |
public Object doInTransaction(TransactionStatus arg0) { |
72 |
|
|
73 |
6 |
List list = jpaTemplate.find(QUERY_STRING, processorName, messageId); |
74 |
6 |
if (list.isEmpty()) { |
75 |
0 |
MessageProcessed processed = new MessageProcessed(); |
76 |
0 |
processed.setProcessorName(processorName); |
77 |
0 |
processed.setMessageId(messageId); |
78 |
0 |
jpaTemplate.persist(processed); |
79 |
0 |
jpaTemplate.flush(); |
80 |
0 |
return Boolean.FALSE; |
81 |
|
} else { |
82 |
6 |
return Boolean.TRUE; |
83 |
|
} |
84 |
|
} |
85 |
|
}); |
86 |
6 |
return rc.booleanValue(); |
87 |
|
} |
88 |
|
} |