Revision 44544
Added by Claudio Atzori over 7 years ago
ResultSetClientFactory.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.resultset.client; |
2 | 2 |
|
3 |
import javax.xml.ws.BindingProvider; |
|
3 | 4 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
4 | 5 |
|
5 | 6 |
import org.apache.commons.logging.Log; |
6 | 7 |
import org.apache.commons.logging.LogFactory; |
8 |
import org.apache.cxf.endpoint.Client; |
|
9 |
import org.apache.cxf.frontend.ClientProxy; |
|
10 |
import org.apache.cxf.transport.http.HTTPConduit; |
|
11 |
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; |
|
7 | 12 |
import org.springframework.beans.factory.annotation.Required; |
8 | 13 |
|
9 | 14 |
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils; |
... | ... | |
22 | 27 |
*/ |
23 | 28 |
private static final Log log = LogFactory.getLog(ResultSetClientFactory.class); |
24 | 29 |
|
30 |
private final static long DEFAULT_CONNECT_TIMEOUT = 10000; |
|
31 |
|
|
32 |
private final static long DEFAULT_REQUEST_TIMEOUT = 60000; |
|
33 |
|
|
34 |
private final static int DEFAULT_PAGE_SIZE = 100; |
|
35 |
|
|
25 | 36 |
/** |
26 | 37 |
* used to resolve the epr references to the service endpoint |
27 | 38 |
*/ |
... | ... | |
38 | 49 |
private int pageSize; |
39 | 50 |
|
40 | 51 |
/** |
41 |
* actual timeout
|
|
52 |
* request timeout
|
|
42 | 53 |
*/ |
43 | 54 |
private long timeout; |
44 | 55 |
|
56 |
/** |
|
57 |
* request timeout |
|
58 |
*/ |
|
59 |
private long connectTimeout; |
|
60 |
|
|
45 | 61 |
public ResultSetClientFactory() { |
46 |
log.info("creating new ResultSetClientIterableFactory with default pageSize and timeout");
|
|
62 |
this(DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
|
|
47 | 63 |
} |
48 | 64 |
|
49 | 65 |
/** |
... | ... | |
53 | 69 |
* @throws IllegalArgumentException |
54 | 70 |
*/ |
55 | 71 |
public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException { |
56 |
if (pageSize <= 0 || timeout <= 0) |
|
57 |
throw new IllegalArgumentException("parameters pageSize and timeout must be greater than zero"); |
|
72 |
this(pageSize, timeout, DEFAULT_CONNECT_TIMEOUT); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* |
|
77 |
* @param pageSize |
|
78 |
* @param timeout |
|
79 |
* time to wait for server response before throwing a timeout exception |
|
80 |
* @param connectTimeout |
|
81 |
* time to wait for server to accept the connection before throwing a connection timeout exception |
|
82 |
* @throws IllegalArgumentException |
|
83 |
*/ |
|
84 |
public ResultSetClientFactory(int pageSize, long timeout, long connectTimeout) throws IllegalArgumentException { |
|
85 |
if (pageSize <= 0 || timeout <= 0 || connectTimeout <= 0) { |
|
86 |
throw new IllegalArgumentException("parameters pageSize, timeout and connectTimeout must be greater than zero"); |
|
87 |
} |
|
88 |
log.info(String.format("creating new ResultSetClientIterableFactory with pageSize (%s), connect timeout (%s) and connect timeout (%s)", |
|
89 |
pageSize, timeout, connectTimeout)); |
|
58 | 90 |
this.pageSize = pageSize; |
59 | 91 |
this.timeout = timeout; |
92 |
this.connectTimeout = connectTimeout; |
|
60 | 93 |
} |
61 | 94 |
|
62 | 95 |
/** |
... | ... | |
67 | 100 |
*/ |
68 | 101 |
@Override |
69 | 102 |
public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) { |
70 |
final ResultSetService resultSet = serviceResolver.getService(ResultSetService.class, epr);
|
|
103 |
final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
|
|
71 | 104 |
final String rsId = serviceResolver.getResourceIdentifier(epr); |
72 | 105 |
|
73 | 106 |
//using given pageSize and default timeout |
74 |
return new IterableResultSetClient(resultSet, rsId, pageSize, timeout);
|
|
107 |
return new IterableResultSetClient(resultSet, rsId, pageSize, getTimeout());
|
|
75 | 108 |
} |
76 | 109 |
|
77 | 110 |
/** |
... | ... | |
81 | 114 |
*/ |
82 | 115 |
@Override |
83 | 116 |
public IterableResultSetClient getClient(W3CEndpointReference epr) { |
84 |
final ResultSetService resultSet = serviceResolver.getService(ResultSetService.class, epr);
|
|
117 |
final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
|
|
85 | 118 |
final String rsId = serviceResolver.getResourceIdentifier(epr); |
86 | 119 |
|
87 |
//using default pageSize and timeout |
|
88 |
return new IterableResultSetClient(resultSet, rsId, pageSize, timeout);
|
|
120 |
//using default pageSize and timeouts
|
|
121 |
return new IterableResultSetClient(resultSet, rsId, getPageSize(), getTimeout());
|
|
89 | 122 |
} |
90 | 123 |
|
124 |
private ResultSetService getResultSetService(final W3CEndpointReference epr, final long connectTimeout, final long requestTimeout) { |
|
125 |
final ResultSetService service = serviceResolver.getService(ResultSetService.class, epr); |
|
126 |
|
|
127 |
if (log.isDebugEnabled()) { |
|
128 |
log.debug(String.format("creting resultSet service stub with connectTimeout(%s), requestTimeout(%s)", connectTimeout, requestTimeout)); |
|
129 |
} |
|
130 |
|
|
131 |
final Client client = ClientProxy.getClient(service); |
|
132 |
final HTTPConduit http = (HTTPConduit) client.getConduit(); |
|
133 |
final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); |
|
134 |
|
|
135 |
httpClientPolicy.setConnectionTimeout(connectTimeout); |
|
136 |
httpClientPolicy.setAllowChunking(false); |
|
137 |
httpClientPolicy.setReceiveTimeout(requestTimeout); |
|
138 |
|
|
139 |
http.setClient(httpClientPolicy); |
|
140 |
|
|
141 |
return service; |
|
142 |
} |
|
143 |
|
|
91 | 144 |
/** |
92 | 145 |
* |
93 | 146 |
* @param stringEpr |
... | ... | |
137 | 190 |
this.timeout = timeout; |
138 | 191 |
} |
139 | 192 |
|
193 |
public long getConnectTimeout() { |
|
194 |
return connectTimeout; |
|
195 |
} |
|
196 |
|
|
197 |
public void setConnectTimeout(final long connectTimeout) { |
|
198 |
this.connectTimeout = connectTimeout; |
|
199 |
} |
|
140 | 200 |
} |
Also available in: Unified diff
setting http connection and request timeouts