liferay调度器-定时任务

参考博客:(1)IT人生录,网址:http://www.huqiwen.com/2012/10/22/liferay-6-1-development-study-11-use-scheduler/

                  (2)步行者,网址 http://blog.youkuaiyun.com/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-entry>
<scheduler-event-listener-class>
com.xxx.xxx.MyMessageListener
</scheduler-event-listener-class>
       <trigger>
           <simple>
              <simple-trigger-value>15</simple-trigger-value>
              <time-unit>minute</time-unit>
           </simple>
<!--
<cron>
<cron-trigger-value>0 0 12? * WED</cron-trigger-value>
</cron>
 -->
        </trigger>
</scheduler-entry>


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

0 0 12? * WED  为每个周二的12点。
seconds, minutes, hours, based on the days; months; on week days; year (optional)

秒  分 时  天;月;周天;年   

(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);
        
    }
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值