RE: Scheduled Job to check Dates

Nikolaos Kroustalakis, modified 5 Years ago. New Member Posts: 14 Join Date: 7/29/19 Recent Posts
I have a Maven Project in Liferay Portal 6.2.I have the following table:

CREATE TABLE DISCIPLINARYAUDIT
(
  DISCIPLINARYAUDITID NUMBER(19, 0) NOT NULL
, DISCIPLINARYAUDITTYPE VARCHAR2(255 CHAR) NOT NULL
, REVOCATIONORCOMPLETION NUMBER(1, 0)
, STARTDATE DATE
, ENDDATE DATE
)


There is a requirement where, once per day (let's say at 00:00), a scheduled job must run where it'll check the
ENDDATE
with the current Date. If they are equal, then 
REVOCATIONORCOMPLETION
  must change from 0 to 1.
I have added this on
liferay-portlet.xml 
:
   
 <portlet>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<portlet-name>penaltysearch</portlet-name>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<scheduler-entry>
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<scheduler-event-listener-class>gr.yptp.hcg.pen.Utils.PenaltySearchUtil</scheduler-event-listener-class>
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<trigger>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<cron>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<!-- Fire at 00:00 every day -->
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<cron-trigger-value>0 0 0 1/1 * ? *</cron-trigger-value>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</cron>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</trigger>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</scheduler-entry>
&nbsp;&nbsp; &nbsp;</portlet>
But I am having a hard time implementing the listener where the check is going to be made.

Any help is really appreciated.Thank you in advance.
thumbnail
Dominik Marks, modified 5 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts
Creating a scheduler should be possible. What are your questions? What problems do you have? If you give more information on the concrete problems it may be easier to help you.
Nikolaos Kroustalakis, modified 5 Years ago. New Member Posts: 14 Join Date: 7/29/19 Recent Posts
I don't know exactly how to continue with the listener.
thumbnail
Dominik Marks, modified 5 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts
You have to implement a MessageListener class. So your gr.yptp.hcg.pen.Utils.PenaltySearchUti should implement MessageListener or extend BaseMessageListener. Everytime the scheduled time is reached the (do)receive-Method is called.

Example from the Liferay 6.2 source code:  https://github.com/liferay/liferay-portal/blob/6.2.x/portal-impl/src/com/liferay/portlet/journal/messaging/CheckArticleMessageListener.java
Nikolaos Kroustalakis, modified 5 Years ago. New Member Posts: 14 Join Date: 7/29/19 Recent Posts
I have the following implementation of the listener:
@Override
&nbsp;&nbsp; &nbsp;public void receive(Message message) throws MessageListenerException {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;logger.info("Scheduler for checking dates on Penalties has started...");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Date currentDate = new Date();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;List<penalty> penalties = service.findAll();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for (Penalty penalty : penalties) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (penalty.endDate == null) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("This date is null");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (currentDate.after(penalty.endDate)) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;penalty.setRevocationOrCompletion(true);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;}</penalty>

And I get this on tomcat:
2020-06-05 14:20:00,021 INFO  (gr.yptp.hcg.pen.Utils.PenaltySearchUtil.java:29).receive - Scheduler for checking dates on Penalties has started...
14:20:00,021 ERROR [liferay/scheduler_dispatch-1][ParallelDestination:74] Unable to process message {destinationName=liferay/scheduler_dispatch, response=null, responseDestinationName=null, responseId=null, payload=null, values={GROUP_NAME=gr.yptp.hcg.pen.Utils.PenaltySearchUtil, PORTLET_ID=penaltysearch_WAR_hcgshipboatcompanyportlet, companyId=0, MESSAGE_LISTENER_UUID=2e03fb6b-7ef0-4df5-b498-a0402eae6efa, groupId=0, DESTINATION_NAME=liferay/scheduler_dispatch, MESSAGE_LISTENER_CLASS_NAME=gr.yptp.hcg.pen.Utils.PenaltySearchUtil, EXCEPTIONS_MAX_SIZE=0, JOB_STATE=com.liferay.portal.kernel.scheduler.JobState@2808fcf8, RECEIVER_KEY=com.liferay.portal.kernel.scheduler.messaging.ReceiverKey@b2c8a854, STORAGE_TYPE=MEMORY_CLUSTERED, JOB_NAME=gr.yptp.hcg.pen.Utils.PenaltySearchUtil}}
com.liferay.portal.kernel.messaging.MessageListenerException: java.lang.NullPointerException
        at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:86)
        at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:72)
        at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:71)
        at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:682)
        at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:593)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
        at gr.yptp.hcg.pen.Utils.PenaltySearchUtil.receive(PenaltySearchUtil.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
        at com.sun.proxy.$Proxy1701.receive(Unknown Source)
        at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:77)
        ... 5 more

If you can help me understand what I am doing wrong, I would really appreciate it.
thumbnail
Dominik Marks, modified 5 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts
Maybe you can post the whole class, so that I can see what's on line 34 of the class? Something at that line seems to be null.