1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.scheduler;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.messaging.Destination;
28  import com.liferay.portal.kernel.messaging.DestinationNames;
29  import com.liferay.portal.kernel.messaging.MessageBusUtil;
30  import com.liferay.portal.kernel.messaging.ParallelDestination;
31  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
32  import com.liferay.portal.kernel.scheduler.SchedulerException;
33  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
34  import com.liferay.portlet.communities.messaging.LayoutsLocalPublisherMessageListener;
35  import com.liferay.portlet.communities.messaging.LayoutsRemotePublisherMessageListener;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="SchedulerEngineImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Bruno Farache
44   *
45   */
46  public class SchedulerEngineImpl implements SchedulerEngine {
47  
48      public SchedulerEngineImpl() {
49          Destination layoutsLocalPublisherDestination = new ParallelDestination(
50              DestinationNames.LAYOUTS_LOCAL_PUBLISHER);
51  
52          MessageBusUtil.addDestination(layoutsLocalPublisherDestination);
53  
54          layoutsLocalPublisherDestination.register(
55              new LayoutsLocalPublisherMessageListener());
56  
57          Destination layoutsRemotePublisherDestination = new ParallelDestination(
58              DestinationNames.LAYOUTS_REMOTE_PUBLISHER);
59  
60          MessageBusUtil.addDestination(layoutsRemotePublisherDestination);
61  
62          layoutsRemotePublisherDestination.register(
63              new LayoutsRemotePublisherMessageListener());
64      }
65  
66      public List<SchedulerRequest> getScheduledJobs(String groupName)
67          throws SchedulerException {
68  
69          try {
70              SchedulerRequest schedulerRequest = new SchedulerRequest(
71                  SchedulerRequest.COMMAND_RETRIEVE, null, groupName, null, null,
72                  null, null, null, null);
73  
74              String message = MessageBusUtil.sendSynchronizedMessage(
75                  DestinationNames.SCHEDULER,
76                  JSONFactoryUtil.serialize(schedulerRequest));
77  
78              JSONObject jsonObj = JSONFactoryUtil.createJSONObject(message);
79  
80              return (List<SchedulerRequest>)JSONFactoryUtil.deserialize(
81                  jsonObj.getString("schedulerRequests"));
82          }
83          catch (Exception e) {
84              throw new SchedulerException(e);
85          }
86      }
87  
88      public void schedule(
89          String groupName, String cronText, Date startDate, Date endDate,
90          String description, String destinationName, String messageBody) {
91  
92          SchedulerRequest schedulerRequest = new SchedulerRequest(
93              SchedulerRequest.COMMAND_REGISTER, null, groupName, cronText,
94              startDate, endDate, description, destinationName, messageBody);
95  
96          MessageBusUtil.sendMessage(
97              DestinationNames.SCHEDULER,
98              JSONFactoryUtil.serialize(schedulerRequest));
99      }
100 
101     public void shutdown() {
102         MessageBusUtil.sendMessage(
103             DestinationNames.SCHEDULER,
104             JSONFactoryUtil.serialize(
105                 new SchedulerRequest(SchedulerRequest.COMMAND_SHUTDOWN)));
106     }
107 
108     public void start() {
109     }
110 
111     public void unschedule(String jobName, String groupName) {
112         SchedulerRequest schedulerRequest = new SchedulerRequest(
113             SchedulerRequest.COMMAND_UNREGISTER, jobName, groupName);
114 
115         MessageBusUtil.sendMessage(
116             DestinationNames.SCHEDULER,
117             JSONFactoryUtil.serialize(schedulerRequest));
118     }
119 
120 }