liferay调度器-定时任务
參考博客:(1)IT人生錄,網址:http://www.huqiwen.com/2012/10/22/liferay-6-1-development-study-11-use-scheduler/
???????? ? ? ? ?? (2)步行者,網址 http://blog.csdn.net/hantiannan/article/details/6739875
(1)在portlet中,自動添加監聽器
job任務系統中經常用到,liferay已經集成了Quartz,寫job很方便,只需繼承MessageListener接口即可。
public class MyMessageListener implements MessageListener{?? ?@Override
?? ?public void receive(Message message) throws MessageListenerException {
?? ??? ? detailMethod(message);
?? ??? ?
?? ?}
?? ?//具體方法的實現
?? ?private void detailMethod(Message message) {
?? ????
?? ?}
在liferay-portlet.xml配置文件中設置啟動信息???
scheduler-event-listener-class:里面的類為第一步里面編寫的類
simple-trigger-value:里面為調度周期的數值,time-unit為調度周期的單位。上面的意思為每15分鐘執行一次Time-unit:表示周期的周期可以為:day、hour、minute、second、week這幾個單位。
具體形如:
<portlet>
?? ??? ?<portlet-name>182</portlet-name>
?? ??? ?<icon>/html/icons/trash.png</icon>
?? ??? ?<struts-path>trash</struts-path>
?? ??? ?<indexer-class>com.liferay.portlet.trash.util.TrashIndexer</indexer-class>
?? ??? ?<scheduler-entry>
?? ??? ??? ?<scheduler-event-listener-class>com.liferay.mySupport.MyMessageListener1</scheduler-event-listener-class>
?? ??? ??? ?<trigger>
?? ??? ??? ??? ?<simple>
?? ??? ??? ??? ??? ?<property-key>mybatch.cron.test</property-key>
?? ??? ??? ??? ??? ?<time-unit>second</time-unit>
?? ??? ??? ??? ?</simple>
?? ??? ??? ?</trigger>
?? ??? ?</scheduler-entry>
?? ????
?? ??? ?<control-panel-entry-category>site_administration.content</control-panel-entry-category>
?? ??? ?<control-panel-entry-weight>30.0</control-panel-entry-weight>
?? ??? ?<control-panel-entry-class>com.liferay.portlet.trash.TrashControlPanelEntry</control-panel-entry-class>
?? ??? ?<preferences-owned-by-group>true</preferences-owned-by-group>
?? ??? ?<use-default-template>false</use-default-template>
?? ??? ?<scopeable>true</scopeable>
?? ??? ?<private-request-attributes>false</private-request-attributes>
?? ??? ?<private-session-attributes>false</private-session-attributes>
?? ??? ?<header-portlet-css>/html/portlet/document_library/css/main.css</header-portlet-css>
?? ??? ?<header-portlet-css>/html/portlet/message_boards/css/main.css</header-portlet-css>
?? ??? ?<header-portlet-css>/html/portlet/trash/css/main.css</header-portlet-css>
?? ??? ?<css-class-wrapper>portlet-trash</css-class-wrapper>
?? ?</portlet>
對于cron形式:
其中在portal-impl中的portal.properties中
??? mybatch.cron.test=3
(2)直接后臺調用添加監聽器(重要)
? portal-impl/src/messaging-misc-spring.xml
中,添加對應的配置:
<?xml version="1.0"?>
<beans
?? ?default-destroy-method="destroy"
?? ?default-init-method="afterPropertiesSet"
?? ?xmlns="http://www.springframework.org/schema/beans"
?? ?xmlns:util="http://www.springframework.org/schema/util"
?? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
>
?? ?<!-- Destinations? 當值為liferay/task時,目的值 destination.task-->
。。。。
? <!-- zhou add? -->
??? <bean id="destination.task" class="com.liferay.portal.kernel.messaging.SerialDestination">
??????? <property name="name" value="liferay/task" />
??? </bean>
。。。
?<!-- Listeners ? ? 監聽器id為messageListener.setup_listener時,類為,,, -->
?? <!-- zhou add? -->
<bean id="messageListener.setup_listener" class="com.liferay.mySupport.SetupMessagingImplTest" />
?? ?
???
?? ?<!-- Configurator -->
?? ?<bean id="messagingConfigurator.misc" class="com.liferay.portal.kernel.messaging.config.DefaultMessagingConfigurator">
?? ??? ?<property name="destinations">
?? ??? ??? ?<list>
......
?? ??? ??? ??? ?<!-- zhou add? -->
?? ??? ??? ??? ?<ref bean="destination.task" />
?? ??? ??? ?</list>
?? ??? ?</property>
?? ??? ?<property name="messageBus">
?? ??? ??? ?<ref bean="com.liferay.portal.kernel.messaging.MessageBus" />
?? ??? ?</property>
?? ??? ?<property name="messageListeners">
?? ??? ??? ?<map key-type="java.lang.String" value-type="java.util.List">
?? ??? ??? ??? 。。。。
?? ??? ??? ??? ?<!-- -zhou add -->
?? ??? ??? ??? ?<entry key="liferay/task">
?? ??? ??? ??? ??? ?<list value-type="com.liferay.portal.kernel.messaging.MessageListener">
?? ??? ??? ??? ??? ??? ?<ref bean="messageListener.setup_listener" />
?? ??? ??? ??? ??? ?</list>
?? ??? ??? ??? ?</entry>
?? ??? ??? ???? 。。。。
?? ??? ??? ?</map>
?? ??? ?</property>
?? ?</bean>
</beans>
觸發程序為:
??????? Message message = new Message();
?? ??? ?
?? ??? ?JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
?? ??? ?jsonObject.put("command", "success ----test command");
?? ??? ?message.setPayload(jsonObject.toString());
?? ??? ?//MessageBusUtil.sendMessage(DestinationNames.LIVE_USERS, message);
? // liferay/task可為portal-service com.liferay.portal.kernel.messaging中DestinationNames,具體為:public static final String LIVE_USERS = "liferay/live_users";?? ???? MessageBusUtil.sendMessage("liferay/task", message);
即可傳到SetupMessagingImplTest監聽器,其為:
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.messaging.BaseMessageListener;
import com.liferay.portal.kernel.messaging.Message;
public class SetupMessagingImplTest? extends BaseMessageListener {
?? ?@Override
?? ?protected void doReceive(Message message) throws Exception {
??
?? ??? ?String payload = (String)message.getPayload();
?? ??? ?JSONObject jsonObject = JSONFactoryUtil.createJSONObject(payload);
?? ??? ?String command = jsonObject.getString("command");
?? ???? System.out.println("message="+message);
?? ???? System.out.println("command="+command);
?? ??? ?
?? ?}
}
總結
以上是生活随笔為你收集整理的liferay调度器-定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fcpx插件:童年印象回忆复古视觉特效和
- 下一篇: 一道[CSCCTF 2019 Qual]