1
22
23 package com.liferay.portal.scheduler.quartz;
24
25 import com.liferay.portal.kernel.json.JSONFactoryUtil;
26 import com.liferay.portal.kernel.json.JSONObject;
27 import com.liferay.portal.kernel.messaging.MessageBusUtil;
28 import com.liferay.portal.kernel.messaging.MessageListener;
29 import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
30
31 import java.util.List;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
42 public class QuartzMessageListener implements MessageListener {
43
44 public void receive(Object message) {
45 throw new UnsupportedOperationException();
46 }
47
48 public void receive(String message) {
49 try {
50 doReceive(message);
51 }
52 catch (Exception e) {
53 _log.error("Unable to process message " + message, e);
54 }
55 }
56
57 protected void doReceive(String message) throws Exception {
58 JSONObject jsonObj = JSONFactoryUtil.createJSONObject(message);
59
60 String responseDestination = jsonObj.getString(
61 "lfrResponseDestination");
62 String responseId = jsonObj.getString("lfrResponseId");
63
64 jsonObj.remove("lfrResponseDestination");
65 jsonObj.remove("lfrResponseId");
66
67 SchedulerRequest schedulerRequest =
68 (SchedulerRequest)JSONFactoryUtil.deserialize(jsonObj);
69
70 String command = schedulerRequest.getCommand();
71
72 if (command.equals(SchedulerRequest.COMMAND_REGISTER)) {
73 QuartzSchedulerEngineUtil.schedule(
74 schedulerRequest.getGroupName(), schedulerRequest.getCronText(),
75 schedulerRequest.getStartDate(), schedulerRequest.getEndDate(),
76 schedulerRequest.getDescription(),
77 schedulerRequest.getDestination(),
78 schedulerRequest.getMessageBody());
79 }
80 else if (command.equals(SchedulerRequest.COMMAND_RETRIEVE)) {
81 doCommandRetrieve(
82 responseDestination, responseId, schedulerRequest);
83 }
84 else if (command.equals(SchedulerRequest.COMMAND_SHUTDOWN)) {
85 QuartzSchedulerEngineUtil.shutdown();
86 }
87 else if (command.equals(SchedulerRequest.COMMAND_UNREGISTER)) {
88 QuartzSchedulerEngineUtil.unschedule(
89 schedulerRequest.getJobName(), schedulerRequest.getGroupName());
90 }
91 }
92
93 protected void doCommandRetrieve(
94 String responseDestination, String responseId,
95 SchedulerRequest schedulerRequest)
96 throws Exception {
97
98 List<SchedulerRequest> schedulerRequests =
99 QuartzSchedulerEngineUtil.getScheduledJobs(
100 schedulerRequest.getGroupName());
101
102 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
103
104 jsonObj.put("lfrResponseId", responseId);
105 jsonObj.put(
106 "schedulerRequests", JSONFactoryUtil.serialize(schedulerRequests));
107
108 MessageBusUtil.sendMessage(responseDestination, jsonObj.toString());
109 }
110
111 private static Log _log = LogFactory.getLog(QuartzMessageListener.class);
112
113 }