1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Liferayのバッチの作成

Last updated at Posted at 2019-09-27

#はじめに
定期的に実行するバッチ処理はシステムよくあります。
Javaのスケジューラのライブラリとしてquartzは有名です。
quartz:http://www.quartz-scheduler.org/

LiferayのスケジューラもQuaryzを採用しております。

Liferayの中でやり方を簡単にまとめます。

#QuartzのCron設定フォーマット
image.png

出典:http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

#QuartzのCron設定サンプル
image.png

出典:http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

#Liferayのバッチ実装必要なメソッド

  • activate:OSGiモジュール起動処理
  • deactivate:OSGiモジュール停止処理
  • receive:ビジネスロジック

#サンプル

TestBatch.java
package com.test.batch;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.DestinationNames;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.portal.kernel.scheduler.SchedulerEngineHelper;
import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
import com.liferay.portal.kernel.scheduler.TimeUnit;
import com.liferay.portal.kernel.scheduler.Trigger;
import com.liferay.portal.kernel.scheduler.TriggerFactoryUtil;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, property = {
}, service =TestBatch.class)
public class TestBatch implements MessageListener {

	private static Log _log = LogFactoryUtil.getLog(TestBatch.class);

	// バッチ起動間隔
	private static int INTERVAL_TIME = 15;

	@Override
	public void receive(Message message) throws MessageListenerException {
		
		_log.info("バッチ 開始");
		
		// do something
		
		_log.info("バッチ 終了");
	}
	
	@Activate
	@Modified
	protected void activate() {
		String batchClassName = this.getClass().getName();
        // トリガーの設定
		Trigger trigger = TriggerFactoryUtil.createTrigger(batchClassName, batchClassName, INTERVAL_TIME, TimeUnit.MINUTE);
		SchedulerEntryImpl schedulerEntryImpl = new SchedulerEntryImpl(batchClassName, trigger);

		_schedulerEngineHelper.register(this, schedulerEntryImpl, DestinationNames.SCHEDULER_DISPATCH);

		_log.info("Batch is activated.");
	}

	@Deactivate
	protected void deactivate() {
		_schedulerEngineHelper.unregister(this);
	}

	@Reference(unbind = "-")
	private volatile SchedulerEngineHelper _schedulerEngineHelper;

}

#ポイントとなるトリガーの作成
image.png
TriggerFactoryUtilクラスに用意されていますので、簡単に作成できます。

CronTrigger: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
参考記事:https://portal.liferay.dev/docs/7-1/tutorials/-/knowledge_base/t/message-listeners

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?