Project

General

Profile

« Previous | Next » 

Revision 46729

async methods

View differences:

modules/dnet-springboot-apps/trunk/dnet-administration-uis/src/main/java/eu/dnetlib/administration/uis/modules/workflows/WorkflowsModule.java
45 45
import eu.dnetlib.administration.uis.modules.workflows.objects.WorkflowUpdateInfo;
46 46
import eu.dnetlib.clients.dsManager.DsManagerClient;
47 47
import eu.dnetlib.clients.is.InformationServiceClient;
48
import eu.dnetlib.clients.locators.ServiceClientFactory;
49
import eu.dnetlib.clients.msro.MsroClient;
48 50
import eu.dnetlib.msro.workflows.NotificationCondition;
49 51
import eu.dnetlib.msro.workflows.StartMode;
50 52
import eu.dnetlib.msro.workflows.WorkflowsConstants;
53
import eu.dnetlib.services.async.AsyncClientCallback;
54
import eu.dnetlib.services.async.AsyncResponse;
51 55

  
52 56
@RestController
53 57
@RequestMapping("/ajax/wfs")
......
66 70
	@Autowired
67 71
	private WorkflowsUIUtils wfUtils;
68 72

  
73
	@Autowired
74
	private ServiceClientFactory clientFactory;
75

  
69 76
	@Autowired(required = false)
70 77
	private List<ParamValuesFunction> paramValuesFunctions = new ArrayList<>();
71 78

  
......
178 185

  
179 186
	}
180 187

  
188
	@RequestMapping("startWorkflow")
189
	public boolean startWorkflow(@RequestParam final String wfId) throws Exception {
190

  
191
		final MsroClient msro = clientFactory.getClient(MsroClient.class);
192

  
193
		msro.startWorkflow(wfId, null, null, new AsyncClientCallback() {
194

  
195
			@Override
196
			public void onGoing(final AsyncResponse res) {
197
				log.info("ONGOING - " + res.getResponseJson());
198
			}
199

  
200
			@Override
201
			public void onFailed(final AsyncResponse res) {
202
				log.info("FAILED - " + res.getResponseJson());
203
			}
204

  
205
			@Override
206
			public void onDone(final AsyncResponse res) {
207
				log.info("DONE - " + res.getResponseJson());
208
			}
209
		});
210

  
211
		return true;
212
	}
213

  
214
	@RequestMapping("startWorkflowTemplate")
215
	public @ResponseBody boolean startWorkflowTemplate(
216
			@RequestParam final String node,
217
			@RequestParam final String parentWf) throws Exception {
218

  
219
		// TODO
220

  
221
		/*
222
		 * final String profile = isClient.getProfile(parentWf); final Document doc = (new SAXReader()).read(new StringReader(profile));
223
		 * final String family = doc.valueOf("//WORKFLOW_FAMILY"); final int priority =
224
		 * NumberUtils.toInt(doc.valueOf("//WORKFLOW_PRIORITY"), WorkflowsConstants.DEFAULT_WF_PRIORITY); final String dsId =
225
		 * doc.valueOf("//DATASOURCE/@id"); final String iface = doc.valueOf("//DATASOURCE/@interface");
226
		 *
227
		 * final Map<String, String> globalParams = new HashMap<String, String>(); for (final Object o :
228
		 * doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM")) { final Element p = (Element) o; globalParams.put(p.valueOf("@name"),
229
		 * p.getTextTrim()); }
230
		 *
231
		 * final Node n = doc.selectSingleNode("//NODE[@name='" + node + "']"); final Map<String, Object> params = GraphNode.newNode(node,
232
		 * n.valueOf("@type"), graphLoader.calculateParamsForNode(node, globalParams)) .resolveParamsWithNoEnv();
233
		 *
234
		 * if (!params.containsKey("wfTemplateId") || !(params.get("wfTemplateId") instanceof String) || StringUtils.isBlank((String)
235
		 * params.get("wfTemplateId"))) { log.error("wfTemplateId is invalid or missing in profile " + parentWf); throw new
236
		 * DnetGenericException("wfTemplateId is invalid or missing in profile " + parentWf); }
237
		 *
238
		 * if (params.containsKey("wfTemplateParams") && !(params.get("wfTemplateParams") instanceof Map)) { log.error(
239
		 * "wfTemplateParams is invalid in profile " + parentWf); throw new DnetGenericException("wfTemplateParams is invalid in profile " +
240
		 * parentWf); }
241
		 *
242
		 * final String wfTtemplateId = (String) params.get("wfTemplateId");
243
		 *
244
		 * @SuppressWarnings("unchecked") final Map<String, String> wfTtemplateParams = (Map<String, String>)
245
		 * params.get("wfTemplateParams");
246
		 *
247
		 * final MsroClient msro = clientFactory.getClient(MsroClient.class);
248
		 *
249
		 * msro.startWorkflowTemplate(wfTtemplateId, parentWf, node, family, wfTtemplateParams, dsId, iface, null);
250
		 */
251
		return true;
252
	}
253

  
181 254
	@RequestMapping(value = "validValues/{param}", method = RequestMethod.POST)
182 255
	public List<Value> obtainValidValues(@PathVariable final String param, @RequestBody final Map<String, String> params) throws IOException {
183 256

  
modules/dnet-springboot-apps/trunk/dnet-administration-uis/src/main/resources/static/js/wfs/wfs.js
158 158

  
159 159
				var url = '';
160 160
				if (isTemplate) {
161
					url = 'wf/wfTemplate.start?node=' + name + '&parentWf=' + parent;
162
				} else if (isTemplate) {
163
					url = 'wf/wf.start?id=' + wfId;
161
					url = '/ajax/wfs/startWorkflowTemplate?node=' + name + '&parentWf=' + parent;
162
				} else {
163
					url = '/ajax/wfs/startWorkflow?wfId=' + wfId;
164 164
				}
165 165

  
166 166
				$http.get(url).success(function (data) {
......
235 235
	};
236 236

  
237 237
	$scope.executeWf = function (wfId) {
238
		$http.get('wf/wf.start?id=' + wfId).success(function (data) {
238
		$http.get('/ajax/wfs/startWorkflow?wfId=' + wfId).success(function (data) {
239 239
			$scope.currentProcId = data.procId;
240 240
			$scope.showProcModal = true;
241 241
		}).error(function (err) {
modules/dnet-springboot-apps/trunk/dnet-simple-aggregation-worker/src/main/java/eu/dnetlib/msro/workflows/nodes/LaunchWorkflowTemplateJobNode.java
11 11
import eu.dnetlib.clients.locators.ServiceClientFactory;
12 12
import eu.dnetlib.clients.msro.MsroClient;
13 13
import eu.dnetlib.msro.annotations.ProcessNode;
14
import eu.dnetlib.msro.controllers.MsroWorkerController;
14 15
import eu.dnetlib.msro.workflows.Arc;
15 16
import eu.dnetlib.msro.workflows.procs.ProcessAware;
16 17
import eu.dnetlib.msro.workflows.procs.Token;
......
44 45
	@Autowired
45 46
	private ServiceClientFactory serviceClientFactory;
46 47

  
48
	@Autowired
49
	private MsroWorkerController worker;
50

  
47 51
	@Override
48 52
	public final void execute(final Token token) {
49 53
		try {
......
54 58

  
55 59
			final MsroClient msro = serviceClientFactory.getClient(MsroClient.class);
56 60

  
57
			msro.startWorkflowTemplate(getWfTemplateId(), process.getProfileId(), getNodeName(), family, wfTemplateParams, new AsyncClientCallback() {
61
			msro.startWorkflowTemplate(getWfTemplateId(), process.getProfileId(), getNodeName(), family, wfTemplateParams, worker.getBaseUrl(),
62
					new AsyncClientCallback() {
58 63

  
59
				@Override
60
				public void onGoing(final AsyncResponse res) {
61
					log.debug("Child workflow is running");
62
				}
64
						@Override
65
						public void onGoing(final AsyncResponse res) {
66
							log.debug("Child workflow is running");
67
						}
63 68

  
64
				@Override
65
				public void onFailed(final AsyncResponse res) {
66
					log.error("Child workflow is failed");
67
					token.releaseAsFailed("Child workflow is failed");
68
				}
69
						@Override
70
						public void onFailed(final AsyncResponse res) {
71
							log.error("Child workflow is failed");
72
							token.releaseAsFailed("Child workflow is failed");
73
						}
69 74

  
70
				@Override
71
				public void onDone(final AsyncResponse res) {
72
					log.debug("Child workflow has been completed successfully");
73
					token.release(Arc.DEFAULT_ARC);
74
				}
75
			});
75
						@Override
76
						public void onDone(final AsyncResponse res) {
77
							log.debug("Child workflow has been completed successfully");
78
							token.release(Arc.DEFAULT_ARC);
79
						}
80
					});
76 81

  
77 82
		} catch (final Throwable e) {
78 83
			log.error("got exception while launching child workflow", e);
modules/dnet-springboot-apps/trunk/dnet-simple-aggregation-worker/src/main/java/eu/dnetlib/msro/workflows/nodes/LaunchWorkflowJobNode.java
9 9
import eu.dnetlib.clients.locators.ServiceClientFactory;
10 10
import eu.dnetlib.clients.msro.MsroClient;
11 11
import eu.dnetlib.msro.annotations.ProcessNode;
12
import eu.dnetlib.msro.controllers.MsroWorkerController;
12 13
import eu.dnetlib.msro.workflows.Arc;
13 14
import eu.dnetlib.msro.workflows.procs.ProcessAware;
14 15
import eu.dnetlib.msro.workflows.procs.Token;
......
29 30

  
30 31
	private WorkflowProcess process;
31 32

  
33
	@Autowired
34
	private MsroWorkerController worker;
35

  
32 36
	@Override
33 37
	public final void execute(final Token token) {
34 38

  
35 39
		try {
36 40
			final MsroClient msro = serviceClientFactory.getClient(MsroClient.class);
37 41

  
38
			msro.startWorkflow(getWfId(), process.getProfileId(), new AsyncClientCallback() {
42
			msro.startWorkflow(getWfId(), process.getProfileId(), worker.getBaseUrl(), new AsyncClientCallback() {
39 43

  
40 44
				@Override
41 45
				public void onGoing(final AsyncResponse res) {
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/clients/msro/MsroClient.java
20 20
	@Autowired
21 21
	private AsyncClientUtils asyncClientUtils;
22 22

  
23
	public void startWorkflow(final String wfId, final String parent, final AsyncClientCallback callback) {
23
	public void startWorkflow(final String wfId, final String parent, final String localBaseUrl, final AsyncClientCallback callback) {
24 24
		final Workflow wf = new Workflow(wfId, parent);
25
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback);
25
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", localBaseUrl, wf, callback);
26 26
	}
27 27

  
28
	public void startWorkflow(final String wfId, final String parent, final String dsId, final String ifaceId, final AsyncClientCallback callback) {
28
	public void startWorkflow(final String wfId,
29
			final String parent,
30
			final String dsId,
31
			final String ifaceId,
32
			final String localBaseUrl,
33
			final AsyncClientCallback callback) {
29 34
		final Workflow wf = new Workflow(wfId, parent, dsId, ifaceId);
30
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback);
35
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", localBaseUrl, wf, callback);
31 36
	}
32 37

  
33 38
	public void startWorkflowTemplate(final String wfTemplateId,
......
35 40
			final String name,
36 41
			final String family,
37 42
			final Map<String, String> params,
43
			final String localBaseUrl,
38 44
			final AsyncClientCallback callback) {
39 45
		final WorkflowTemplate wf = new WorkflowTemplate(wfTemplateId, parent, name, family, params);
40
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback);
46
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", localBaseUrl, wf, callback);
41 47
	}
42 48

  
43 49
	public void startWorkflowTemplate(final String wfTemplateId,
......
47 53
			final Map<String, String> params,
48 54
			final String dsId,
49 55
			final String ifaceId,
56
			final String localBaseUrl,
50 57
			final AsyncClientCallback callback) {
51 58
		final WorkflowTemplate wf = new WorkflowTemplate(wfTemplateId, parent, name, family, dsId, ifaceId, params);
52
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback);
59
		asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", localBaseUrl, wf, callback);
53 60
	}
54 61

  
55 62
}
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/clients/locators/DefaultServiceClientFactory.java
61 61

  
62 62
	@Override
63 63
	public <T extends BaseServiceClient> T getClient(final Class<T> clazz, final Comparator<ServiceRunningInstance> comparator) {
64
		if (!getClass().isAnnotationPresent(DnetServiceClient.class)) { throw new DnetGenericRuntimeException(
65
				"A required annotation is missing (@DnetServiceClient) in class " + getClass()); }
64
		if (!clazz.isAnnotationPresent(DnetServiceClient.class)) { throw new DnetGenericRuntimeException(
65
				"A required annotation is missing (@DnetServiceClient) in class " + clazz); }
66 66

  
67
		final DnetServiceType type = getClass().getAnnotation(DnetServiceClient.class).value();
67
		final DnetServiceType type = clazz.getAnnotation(DnetServiceClient.class).value();
68 68

  
69 69
		log.debug("searching for service: " + type);
70 70

  
71
		final String xquery = "for $x in collection('/db/DRIVER/dnetService/" + type + ") return $x//RESOURCE_URI/@value/string()";
71
		final String xquery = "for $x in collection('/db/DRIVER/dnetService/" + type + "') return $x//RESOURCE_URI/@value/string()";
72 72

  
73 73
		try {
74 74
			final Optional<ServiceRunningInstance> instance = isClient.find(xquery)
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/BaseService.java
111 111
	}
112 112

  
113 113
	@RequestMapping(value = "async/method/{method}", method = RequestMethod.POST)
114
	public final String asyncMethod(@PathVariable final String method, @RequestParam final String clientBaseUrl, @RequestParam final String jsonParams)
114
	public final String asyncMethod(@PathVariable final String method, @RequestParam final String caller, @RequestBody final String jsonParams)
115 115
			throws DnetGenericException {
116 116

  
117
		log.info("Starting async method: " + method);
118

  
117 119
		if (this instanceof HasAsyncMethods) {
118 120

  
119 121
			final String id = "invocation-" + UUID.randomUUID();
120
			final String url = clientBaseUrl + "/async/response/" + id;
122
			final String url = caller + "/async/response/" + id;
121 123
			final RestTemplate restTemplate = new RestTemplate();
122 124

  
123 125
			new Thread(() -> {
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncClientUtils.java
15 15

  
16 16
	private static final Map<String, AsyncClientCallback> activeRemoteCalls = new HashMap<>();
17 17

  
18
	public void invokeRemoteMethod(final String remoteBaseUrl, final String remoteMethod, final Object o, final AsyncClientCallback callback) {
19
		final String url = remoteBaseUrl + "/async/method/" + remoteMethod;
18
	public void invokeRemoteMethod(final String remoteBaseUrl,
19
			final String remoteMethod,
20
			final String localBaseUrl,
21
			final Object o,
22
			final AsyncClientCallback callback) {
23
		final String url = remoteBaseUrl + "/async/method/" + remoteMethod + "?caller=" + localBaseUrl;
24

  
25
		log.info("invoking async method: " + url);
26

  
20 27
		final String id = (new RestTemplate()).postForObject(url, o, String.class);
21
		activeRemoteCalls.put(id, callback);
28
		if (callback != null) {
29
			activeRemoteCalls.put(id, callback);
30
		}
22 31
	}
23 32

  
24 33
	public boolean processResponse(final String id, final AsyncResponse response) {

Also available in: Unified diff