1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jdbc; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.providers.ConnectException; |
16 | |
import org.mule.providers.TransactedPollingMessageReceiver; |
17 | |
import org.mule.transaction.TransactionCoordination; |
18 | |
import org.mule.umo.UMOComponent; |
19 | |
import org.mule.umo.UMOMessage; |
20 | |
import org.mule.umo.UMOTransaction; |
21 | |
import org.mule.umo.endpoint.UMOEndpoint; |
22 | |
import org.mule.umo.lifecycle.InitialisationException; |
23 | |
import org.mule.umo.provider.UMOConnector; |
24 | |
import org.mule.umo.provider.UMOMessageAdapter; |
25 | |
|
26 | |
import java.sql.Connection; |
27 | |
import java.sql.SQLException; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.List; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class JdbcMessageReceiver extends TransactedPollingMessageReceiver |
35 | |
{ |
36 | |
|
37 | |
protected JdbcConnector connector; |
38 | |
protected String readStmt; |
39 | |
protected String ackStmt; |
40 | |
protected List readParams; |
41 | |
protected List ackParams; |
42 | |
|
43 | |
public JdbcMessageReceiver(UMOConnector connector, |
44 | |
UMOComponent component, |
45 | |
UMOEndpoint endpoint, |
46 | |
String readStmt, |
47 | |
String ackStmt) throws InitialisationException |
48 | |
{ |
49 | 2 | super(connector, component, endpoint); |
50 | 2 | this.setFrequency(((JdbcConnector)connector).getPollingFrequency()); |
51 | 2 | this.setReceiveMessagesInTransaction(false); |
52 | |
|
53 | 2 | this.connector = (JdbcConnector)connector; |
54 | 2 | this.readParams = new ArrayList(); |
55 | 2 | this.readStmt = this.connector.parseStatement(readStmt, this.readParams); |
56 | 2 | this.ackParams = new ArrayList(); |
57 | 2 | this.ackStmt = this.connector.parseStatement(ackStmt, this.ackParams); |
58 | 2 | } |
59 | |
|
60 | |
protected void doDispose() |
61 | |
{ |
62 | |
|
63 | 2 | } |
64 | |
|
65 | |
protected void doConnect() throws Exception |
66 | |
{ |
67 | 0 | Connection con = null; |
68 | |
try |
69 | |
{ |
70 | 0 | con = this.connector.getConnection(); |
71 | |
} |
72 | 0 | catch (Exception e) |
73 | |
{ |
74 | 0 | throw new ConnectException(e, this); |
75 | |
} |
76 | |
finally |
77 | |
{ |
78 | 0 | JdbcUtils.close(con); |
79 | 0 | } |
80 | 0 | } |
81 | |
|
82 | |
protected void doDisconnect() throws ConnectException |
83 | |
{ |
84 | |
|
85 | 0 | } |
86 | |
|
87 | |
public void processMessage(Object message) throws Exception |
88 | |
{ |
89 | 0 | Connection con = null; |
90 | 0 | UMOTransaction tx = TransactionCoordination.getInstance().getTransaction(); |
91 | |
try |
92 | |
{ |
93 | 0 | con = this.connector.getConnection(); |
94 | 0 | UMOMessageAdapter msgAdapter = this.connector.getMessageAdapter(message); |
95 | 0 | UMOMessage umoMessage = new MuleMessage(msgAdapter); |
96 | 0 | if (this.ackStmt != null) |
97 | |
{ |
98 | |
|
99 | 0 | Object[] ackParams = connector.getParams(endpoint, this.ackParams, umoMessage, this.endpoint.getEndpointURI().getAddress()); |
100 | 0 | int nbRows = connector.createQueryRunner().update(con, this.ackStmt, ackParams); |
101 | 0 | if (nbRows != 1) |
102 | |
{ |
103 | 0 | logger.warn("Row count for ack should be 1 and not " + nbRows); |
104 | |
} |
105 | |
} |
106 | 0 | routeMessage(umoMessage, tx, tx != null || endpoint.isSynchronous()); |
107 | |
|
108 | |
} |
109 | 0 | catch (Exception ex) |
110 | |
{ |
111 | 0 | if (tx != null) |
112 | |
{ |
113 | 0 | tx.setRollbackOnly(); |
114 | |
} |
115 | |
|
116 | |
|
117 | 0 | throw ex; |
118 | |
} |
119 | |
finally |
120 | |
{ |
121 | 0 | if (MuleManager.getInstance().getTransactionManager() != null || tx == null) |
122 | |
{ |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | 0 | JdbcUtils.close(con); |
135 | |
} |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
public List getMessages() throws Exception |
140 | |
{ |
141 | 0 | Connection con = null; |
142 | |
try |
143 | |
{ |
144 | |
try |
145 | |
{ |
146 | 0 | con = this.connector.getConnection(); |
147 | |
} |
148 | 0 | catch (SQLException e) |
149 | |
{ |
150 | 0 | throw new ConnectException(e, this); |
151 | 0 | } |
152 | |
|
153 | 0 | Object[] readParams = connector.getParams(endpoint, this.readParams, null, this.endpoint.getEndpointURI().getAddress()); |
154 | 0 | Object results = connector.createQueryRunner().query(con, this.readStmt, readParams, |
155 | |
connector.createResultSetHandler()); |
156 | 0 | return (List)results; |
157 | |
} |
158 | |
finally |
159 | |
{ |
160 | 0 | JdbcUtils.close(con); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
} |