留言板

Using JournalArticleService in startup action component

thumbnail
Abhishek Suthar,修改在7 年前。

Using JournalArticleService in startup action component

New Member 帖子: 18 加入日期: 13-6-26 最近的帖子
Hello Developers,

I have created simple startup action component using OSGI DS annotation. I want to access journal articles when liferay starts.But some how I'm not able to access journal Article service.

Please find my code below,

package com.example.api;
 
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
 
import com.liferay.journal.service.JournalArticleLocalService;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.LifecycleAction;
import com.liferay.portal.kernel.events.LifecycleEvent;
 
@Component(
       
        property={"key=application.startup.events"},
        service = LifecycleAction.class
        )
public class StartupActionapi implements LifecycleAction {
 
    @Override
    public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {
        System.out.println("Im starting man>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println("journel>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println("+++++++++++++++++++++"+_journalArticleLocalService.getArticles().size());
    }
   
    @Reference(cardinality = ReferenceCardinality.OPTIONAL,
            policy = ReferencePolicy.DYNAMIC,
            policyOption = ReferencePolicyOption.GREEDY)
    protected void setJournalArticleLocalService(
        JournalArticleLocalService journalArticleLocalService) {
 
        _journalArticleLocalService = journalArticleLocalService;
    }
   
    protected void unsetJournalArticleLocalService(
            JournalArticleLocalService journalArticleLocalService) {
 
            _journalArticleLocalService = null;
        }
       
   
    private JournalArticleLocalService _journalArticleLocalService;
   
}


Getting first two system outs in logs BUT,
I'm getting nullpointer on below line,
 System.out.println("+++++++++++++++++++++"+_journalArticleLocalService.getArticles().size());
thumbnail
Eduardo P. García,修改在3 年前。

RE: Using JournalArticleService in startup action component

Regular Member 帖子: 157 加入日期: 12-3-16 最近的帖子
Hi Abhishek,

If you want to make sure the JournalService is available when your module starts, then you shouldn't declare your dependency with the journalService as "OPTIONAL".

Regards
thumbnail
Abhishek Suthar,修改在7 年前。

RE: Using JournalArticleService in startup action component

New Member 帖子: 18 加入日期: 13-6-26 最近的帖子
Eduardo P. Garcia:
Hi Abhishek,

If you want to make sure the JournalService is available when your module starts, then you shouldn't declare your dependency with the journalService as "OPTIONAL".

Regards


When I change it to MANDATORY. I'm not getting first two sysouts in logs. I think bundle not even starts.

When I change to "OPTIONAL" it shows the first two sysouts and then null pointer.