Common Listener

If the job processes the files of the job server and deletes the files after the processing is completed, consider using each node to perform the cleaning task. This type of task is simple to implement, and there is no need to consider whether the global distributed task is completed. You should try to use this type of listener.

  1. public class MyJobListener implements ElasticJobListener {
  2. @Override
  3. public void beforeJobExecuted(ShardingContexts shardingContexts) {
  4. // do something ...
  5. }
  6. @Override
  7. public void afterJobExecuted(ShardingContexts shardingContexts) {
  8. // do something ...
  9. }
  10. @Override
  11. public String getType() {
  12. return "simpleJobListener";
  13. }
  14. }

Distributed Listener

If the job processes database data, only one node needs to complete the data cleaning task after the processing is completed. This type of task is complicated to process and needs to synchronize the status of the job in a distributed environment. Timeout settings are provided to avoid deadlocks caused by job out of sync. It should be used with caution.

  1. public class MyDistributeOnceJobListener extends AbstractDistributeOnceElasticJobListener {
  2. public TestDistributeOnceElasticJobListener(long startTimeoutMills, long completeTimeoutMills) {
  3. super(startTimeoutMills, completeTimeoutMills);
  4. }
  5. @Override
  6. public void doBeforeJobExecutedAtLastStarted(ShardingContexts shardingContexts) {
  7. // do something ...
  8. }
  9. @Override
  10. public void doAfterJobExecutedAtLastCompleted(ShardingContexts shardingContexts) {
  11. // do something ...
  12. }
  13. @Override
  14. public String getType() {
  15. return "distributeOnceJobListener";
  16. }
  17. }

Add SPI implementation

Put JobListener implementation to module infra-common, resources/META-INF/services/org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener