Defining the Job
Create a new java class SendRankingJob in the package de/hybris/platform/cuppytrail/jobs:
/*
* [y] hybris Platform
*
* Copyright (c) 2000-2011 hybris AG
* All rights reserved.
*
* This software is the confidential and proprietary information of hybris
* ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the
* license agreement you entered into with hybris.
*
*
*/
package
de.hybris.platform.cuppytrail.jobs;
import
de.hybris.platform.cronjob.enums.CronJobResult;
import
de.hybris.platform.cronjob.enums.CronJobStatus;
import
de.hybris.platform.cronjob.model.CronJobModel;
import
de.hybris.platform.cuppy.model.PlayerModel;
import
de.hybris.platform.cuppy.services.MailService;
import
de.hybris.platform.cuppy.services.PlayerService;
import
de.hybris.platform.cuppy.services.RankingData;
import
de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;
import
de.hybris.platform.servicelayer.cronjob.PerformResult;
import
java.util.List;
import
org.apache.log4j.Logger;
public
class
SendRankingJob
extends
AbstractJobPerformable<CronJobModel>
{
private
static
final
Logger LOG = Logger.getLogger(SendRankingJob.
class
);
private
PlayerService playerService;
private
MailService mailService;
@Override
public
PerformResult perform(
final
CronJobModel cronJob)
{
LOG.info(
"Sending ranking mails"
);
final
List<RankingData> rankings = playerService.getRankings();
if
(rankings.isEmpty())
{
LOG.info(
"No competitions have changed, skipping send of ranking mails"
);
return
new
PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
for
(
final
PlayerModel player : playerService.getAllPlayers())
{
final
List<RankingData> playerRankings = playerService.filterRankingsForPlayer(rankings, player);
if
(!playerRankings.isEmpty() && player.isSendNewsletter())
{
mailService.sendRankingMail(player, playerRankings);
}
}
return
new
PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
public
void
setPlayerService(
final
PlayerService playerService)
{
this
.playerService = playerService;
}
public
void
setMailService(
final
MailService trailMailService)
{
this
.mailService = trailMailService;
}
}
|
The new JobPerformable has to be defined as a Spring bean in cuppytrail-spring.xml.
Add the following line at the bottom of the file, but inside the beans-tag:
<bean id=
"sendRankingJob"
class
=
"de.hybris.platform.cuppytrail.jobs.SendRankingJob"
autowire=
"byName"
/>
|
Rebuild the hybris Platform by calling ant
in the $/{HYBRIS_BIN_DIR}/platform
directory.
Run a system update with only essential data checked - during the phase of essential data creation, for each Spring definition of a class implementing the JobPerformable
interface, a ServicelayerJob instance gets created and the code attribute of the job is set to the name of the Spring bean.
You can check in the FlexibleSearch console: http://localhost:9001/console/flexsearch that the new item was created by executing the following query:
select {code} from {servicelayerjob} where {code} = 'sendRankingJob'
Create the CronJob and the Trigger
To create the CronJob and the Trigger, you can either:
-
You can go in the hybris Admin Console to the Console tab select ImpEx Import and execute the following impex-script there by clicking on the Import Content button
INSERT_UPDATE CronJob; code[unique=
true
];job(code);singleExecutable;sessionLanguage(isocode)
;sendRankingCronJob;sendRankingJob;
false
;de
INSERT_UPDATE Trigger;cronjob(code)[unique=
true
];cronExpression
#% afterEach: impex.getLastImportedItem().setActivationTime(
new
Date());
; sendRankingCronJob;
0
0
0
* * ?
A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of allowed special characters for that field.
-
Seconds
YES
0-59
, - * /
Minutes
YES
0-59
, - * /
Hours
YES
0-23
, - * /
Day of month
YES
1-31
, - * ? / L W
Month
YES
1-12 or JAN-DEC
, - * /
Day of week
YES
1-7 or SUN-SAT
, - * ? / L #
Year
NO
empty, 1970-2099
, - * /
Quartz cron trigger is used, see Quartz Scheduler for more details.
Any changes you are making for testing can easily be redeployed or reexecuted.
- OR create the file resources/impex/essentialdataJobs.impex with the same content. Further changes are only taken into account after a server-restart and a system update (with only essential data checked). Changes made to resources after being loaded as classloader-resources are not visible.
Test the job
-
- Simply log in into the hmc
- Open the cuppy-section and edit a player
- Set the newsletter-attribute to true
- Change the eMail address of this player to your email address (if you set up email previously)
- Execute the job manually
- In the left tree, go to System | CronJobs
- Select sendRankingCronJob
- Click 'Start CronJob now'