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