1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jdbc.sqlstrategy; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
17 | |
import org.mule.api.transaction.Transaction; |
18 | |
import org.mule.transaction.TransactionCoordination; |
19 | |
import org.mule.transport.jdbc.JdbcConnector; |
20 | |
import org.mule.transport.jdbc.JdbcUtils; |
21 | |
import org.mule.util.ArrayUtils; |
22 | |
|
23 | |
import java.sql.Connection; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.apache.log4j.Logger; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class SelectSqlStatementStrategy implements SqlStatementStrategy |
34 | |
{ |
35 | 0 | protected transient Logger logger = Logger.getLogger(getClass()); |
36 | |
|
37 | |
public MuleMessage executeStatement(JdbcConnector connector, ImmutableEndpoint endpoint, |
38 | |
MuleEvent event, long timeout) throws Exception |
39 | |
{ |
40 | 0 | logger.debug("Trying to receive a message with a timeout of " + timeout); |
41 | |
|
42 | 0 | String[] stmts = connector.getReadAndAckStatements(endpoint); |
43 | |
|
44 | |
|
45 | 0 | String readStmt = stmts[0]; |
46 | 0 | String ackStmt = stmts[1]; |
47 | |
|
48 | |
|
49 | 0 | List readParams = new ArrayList(); |
50 | 0 | List ackParams = new ArrayList(); |
51 | |
|
52 | |
|
53 | 0 | readStmt = connector.parseStatement(readStmt, readParams); |
54 | 0 | ackStmt = connector.parseStatement(ackStmt, ackParams); |
55 | |
|
56 | 0 | Connection con = null; |
57 | 0 | long t0 = System.currentTimeMillis(); |
58 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
59 | |
try |
60 | |
{ |
61 | 0 | con = connector.getConnection(); |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | if (timeout < 0) |
66 | |
{ |
67 | 0 | timeout = Long.MAX_VALUE; |
68 | |
} |
69 | |
Object result; |
70 | |
|
71 | |
|
72 | |
do |
73 | |
{ |
74 | |
|
75 | 0 | Object[] params = connector.getParams(endpoint, readParams, |
76 | |
event != null ? event.getMessage() : null, |
77 | |
endpoint.getEndpointURI().getAddress()); |
78 | |
|
79 | 0 | if (logger.isDebugEnabled()) |
80 | |
{ |
81 | 0 | logger.debug("SQL QUERY: " + readStmt + ", params = " + ArrayUtils.toString(params)); |
82 | |
} |
83 | |
|
84 | |
|
85 | 0 | result = connector.getQueryRunnerFor(endpoint).query(con, readStmt, |
86 | |
connector.getResultSetHandler(), params); |
87 | |
|
88 | 0 | if (result != null) |
89 | |
{ |
90 | 0 | if (logger.isDebugEnabled()) |
91 | |
{ |
92 | 0 | logger.debug("SQL query received a result: " + result); |
93 | |
} |
94 | 0 | else if (logger.isInfoEnabled()) |
95 | |
{ |
96 | 0 | logger.info("SQL query received a result"); |
97 | |
} |
98 | |
break; |
99 | |
} |
100 | 0 | long sleep = Math.min(connector.getPollingFrequency(), |
101 | |
timeout - (System.currentTimeMillis() - t0)); |
102 | 0 | if (sleep > 0) |
103 | |
{ |
104 | 0 | if (logger.isDebugEnabled()) |
105 | |
{ |
106 | 0 | logger.debug("No results, sleeping for " + sleep); |
107 | |
} |
108 | 0 | Thread.sleep(sleep); |
109 | |
} |
110 | |
else |
111 | |
{ |
112 | 0 | logger.debug("Timeout"); |
113 | 0 | JdbcUtils.rollbackAndClose(con); |
114 | 0 | return null; |
115 | |
} |
116 | 0 | } while (true); |
117 | |
|
118 | |
|
119 | 0 | if (ackStmt != null) |
120 | |
{ |
121 | 0 | Object[] params = connector.getParams(endpoint, ackParams, |
122 | |
new DefaultMuleMessage(result, (Map)null, connector.getMuleContext()), ackStmt); |
123 | 0 | if (logger.isDebugEnabled()) |
124 | |
{ |
125 | 0 | logger.debug("SQL UPDATE: " + ackStmt + ", params = " + ArrayUtils.toString(params)); |
126 | |
} |
127 | 0 | int nbRows = connector.getQueryRunnerFor(endpoint).update(con, ackStmt, params); |
128 | 0 | if (nbRows != 1) |
129 | |
{ |
130 | 0 | logger.warn("Row count for ack should be 1 and not " + nbRows); |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
|
135 | 0 | MuleMessage message = null; |
136 | 0 | if (event != null) |
137 | |
{ |
138 | 0 | message = new DefaultMuleMessage(result, event.getMessage(), connector.getMuleContext()); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | message = new DefaultMuleMessage(result, connector.getMuleContext()); |
143 | |
} |
144 | |
|
145 | |
|
146 | 0 | if (tx == null) |
147 | |
{ |
148 | 0 | JdbcUtils.commitAndClose(con); |
149 | |
} |
150 | |
|
151 | 0 | return message; |
152 | |
} |
153 | 0 | catch (Exception e) |
154 | |
{ |
155 | 0 | if (tx == null) |
156 | |
{ |
157 | 0 | JdbcUtils.rollbackAndClose(con); |
158 | |
} |
159 | 0 | throw e; |
160 | |
} |
161 | |
} |
162 | |
} |