1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package org.springframework.remoting.httpinvoker;
|
10
|
|
11
|
|
12
|
import java.time.LocalDateTime;
|
13
|
import java.time.format.DateTimeFormatter;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.aopalliance.intercept.MethodInvocation;
|
18
|
import org.apache.logging.log4j.LogManager;
|
19
|
import org.apache.logging.log4j.Logger;
|
20
|
import org.springframework.remoting.support.RemoteInvocation;
|
21
|
import org.springframework.remoting.support.RemoteInvocationResult;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
24
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
25
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
26
|
import eu.etaxonomy.taxeditor.service.IRemoteInvocationTermCacher;
|
27
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
28
|
|
29
|
/**
|
30
|
* Extension of the <code>HttpInvokerProxyFactoryBean</code> to support caching of general CDM entities and terms.
|
31
|
* <p>
|
32
|
* <b>Performance measurement:</b></br>
|
33
|
* Supports measuring the request processing time at the client side. Setting "{@value #PROP_KEY_MEASURE_DURATION}"
|
34
|
* as system parameter enables the measurement. To make the measurements appear for each request in the log, the logging <code>Level</code>
|
35
|
* for <code>HttpInvokerProxyFactoryBean</code> needs to be set to at least {@link org.apache.logging.log4j.Level#INFO}.
|
36
|
* Otherwise the duration is only reported in case of errors.
|
37
|
*
|
38
|
* @author a.kohlbecker
|
39
|
* @since Feb 4, 2020
|
40
|
*/
|
41
|
public class CachingHttpInvokerProxyFactoryBean extends HttpInvokerProxyFactoryBean {
|
42
|
|
43
|
private static final Logger logger = LogManager.getLogger(CachingHttpInvokerProxyFactoryBean.class);
|
44
|
|
45
|
private static final String PROP_KEY_MEASURE_DURATION = "remoting.httpinvoker.measureDuration";
|
46
|
|
47
|
private ICdmEntitySessionManager cdmEntitySessionManager;
|
48
|
|
49
|
private IRemoteInvocationTermCacher remoteInvocationTermCacher;
|
50
|
|
51
|
protected final static Set<String> persistingMethods = new HashSet<>();
|
52
|
|
53
|
protected static boolean measureDuration = false;
|
54
|
|
55
|
static {
|
56
|
persistingMethods.add("merge");
|
57
|
persistingMethods.add("save");
|
58
|
persistingMethods.add("findWithUpdate");
|
59
|
persistingMethods.add("loadWithUpdate");
|
60
|
measureDuration = System.getProperty(PROP_KEY_MEASURE_DURATION) != null;
|
61
|
//measureDuration = true;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
protected RemoteInvocationResult executeRequest(
|
66
|
RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception {
|
67
|
|
68
|
// logger.setLevel(Level.INFO);
|
69
|
|
70
|
if (!(invocation.getMethodName() == null) && !(getServiceUrl() == null) && logger.isDebugEnabled()){
|
71
|
logger.debug("Remote invoking : " + getServiceUrl() + "#" + invocation.getMethodName());
|
72
|
}
|
73
|
|
74
|
if(CdmApplicationState.getCurrentAppConfig() == null){
|
75
|
logger.debug("No application context yet, no point caching!");
|
76
|
return doExecuteRequest(invocation, originalInvocation);
|
77
|
}
|
78
|
|
79
|
// A) if this is a term service call for term lists try to get the terms from the cache
|
80
|
|
81
|
|
82
|
if(ITermService.class.isAssignableFrom(this.getServiceInterface())){
|
83
|
return handleTermRequest(invocation, originalInvocation);
|
84
|
}
|
85
|
// B) handle other service calls
|
86
|
else {
|
87
|
return handleGeneralRequest(invocation, originalInvocation);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
private RemoteInvocationResult handleGeneralRequest(RemoteInvocation invocation,
|
92
|
MethodInvocation originalInvocation) throws Exception {
|
93
|
|
94
|
RemoteInvocationResult invocationResult = doExecuteRequest(invocation, originalInvocation);
|
95
|
if(invocationResult.getValue() != null && !invocationResult.hasException()) {
|
96
|
|
97
|
//Note AM (2022-06-15): the debug information seem to be misleading (also because cache() is not used/implemented anymore)
|
98
|
if(persistingMethods.contains(invocation.getMethodName())) {
|
99
|
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), true));
|
100
|
logger.debug("Entity cached with updating cached data");
|
101
|
} else if(invocationResult.getValue() instanceof UpdateResult){
|
102
|
UpdateResult result = (UpdateResult)invocationResult.getValue();
|
103
|
if(result.isOk()){
|
104
|
logger.debug("Entity from UpdateResult stored in cache with updating cached data" );
|
105
|
try {
|
106
|
cdmEntitySessionManager().load(result, true);
|
107
|
} catch (Exception e) {
|
108
|
String message = "Error when trying to add update result to session cache in TaxEditor";
|
109
|
throw new RuntimeException(message, e);
|
110
|
}
|
111
|
}
|
112
|
} else {
|
113
|
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), false));
|
114
|
logger.debug("Entity cached without updating cached data" );
|
115
|
}
|
116
|
}
|
117
|
cache(invocation, invocationResult);
|
118
|
return invocationResult;
|
119
|
}
|
120
|
|
121
|
public void cache(RemoteInvocation ri, RemoteInvocationResult rir) {
|
122
|
|
123
|
}
|
124
|
|
125
|
private RemoteInvocationResult handleTermRequest(RemoteInvocation invocation, MethodInvocation originalInvocation)
|
126
|
throws Exception {
|
127
|
|
128
|
RemoteInvocationResult invocationResult = null;
|
129
|
if(remoteInvocationTermCacher != null){
|
130
|
invocationResult = remoteInvocationTermCacher.termsFromCache(invocation);
|
131
|
if(invocationResult == null) {
|
132
|
invocationResult = doExecuteRequest(invocation, originalInvocation);
|
133
|
try {
|
134
|
remoteInvocationTermCacher.cacheTerms(invocation, invocationResult);
|
135
|
} catch (Exception e) {
|
136
|
String message = "Error when caching terms in TaxEditor";
|
137
|
throw new RuntimeException(message, e);
|
138
|
}
|
139
|
logger.warn("Term list loaded and cached");
|
140
|
} else {
|
141
|
logger.debug("Term list found in cache, not loaded");
|
142
|
}
|
143
|
} else {
|
144
|
logger.warn("Term list: No IRemoteInvocationTermCacher configured");
|
145
|
invocationResult = doExecuteRequest(invocation, originalInvocation);
|
146
|
}
|
147
|
return invocationResult;
|
148
|
}
|
149
|
|
150
|
private RemoteInvocationResult doExecuteRequest(RemoteInvocation invocation, MethodInvocation originalInvocation)
|
151
|
throws Exception {
|
152
|
|
153
|
double startTime = 0;
|
154
|
if(measureDuration){
|
155
|
startTime = System.currentTimeMillis();
|
156
|
}
|
157
|
try {
|
158
|
RemoteInvocationResult result = super.executeRequest(invocation, originalInvocation);
|
159
|
if(measureDuration){
|
160
|
double duration = System.currentTimeMillis() - startTime;
|
161
|
|
162
|
LocalDateTime date = LocalDateTime.now();
|
163
|
String dateTimeStr = date.format(DateTimeFormatter.ISO_DATE_TIME);
|
164
|
System.out.println(dateTimeStr + ": "+ getServiceUrl() + "#" + invocation.getMethodName() + " [" + duration + " ms]");
|
165
|
logger.info(dateTimeStr + getServiceUrl() + "#" + invocation.getMethodName() + " [" + duration + " ms]");
|
166
|
}
|
167
|
return result;
|
168
|
} catch(Exception e) {
|
169
|
if(measureDuration){
|
170
|
double duration = System.currentTimeMillis() - startTime;
|
171
|
logger.error("(" + e.getClass().getName() + ": \"" + e.getMessage() + "\") interrupts " + getServiceUrl() + "#" + invocation.getMethodName() + " after [" + duration + " ms]");
|
172
|
}
|
173
|
throw e;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
public IRemoteInvocationTermCacher getRemoteInvocationTermCacher() {
|
178
|
return remoteInvocationTermCacher;
|
179
|
}
|
180
|
public void setRemoteInvocationTermCacher(IRemoteInvocationTermCacher remoteInvocationTermCacher) {
|
181
|
this.remoteInvocationTermCacher = remoteInvocationTermCacher;
|
182
|
}
|
183
|
|
184
|
private ICdmEntitySessionManager cdmEntitySessionManager(){
|
185
|
if(cdmEntitySessionManager == null) {
|
186
|
cdmEntitySessionManager =
|
187
|
CdmApplicationState.getCurrentAppConfig().getCdmEntitySessionManager();
|
188
|
}
|
189
|
return cdmEntitySessionManager;
|
190
|
}
|
191
|
}
|