RE: create service context in component class

thumbnail
ali yeganeh, modified 6 Years ago. Regular Member Posts: 148 Join Date: 5/1/19 Recent Posts
Hello
This is my sample code in the Message Listener field
I had the following error
Thanks for helping guide me

              @Component (immediate = true, property = { "cron.expression=15 43 9 * * ?" }, service = MyTaskMessageListener.class)
               public class MyTaskMessageListener extends BaseMessageListene
               {
                                             @Override
                                              protected void doReceive( Message message ) throws Exception
                                              {
                                                                    _log.info( "---------Execute Task--------- " );
                                                                     ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
                                                                   _log.info( serviceContext.getScopeGroupId() ); //
                                             }
            . . .
 Unable to process message {destinationName=liferay/scheduler_dispatch, response=null, responseDestinationName=null, responseId=null, payload=null, values={GROUP_NAME=ir.ivo.portal.core.portlet.MyTaskMessageListener, companyId=0, groupId=0, DESTINATION_NAME=liferay/scheduler_dispatch, EXCEPTIONS_MAX_SIZE=0, JOB_STATE=com.liferay.portal.kernel.scheduler.JobState@7170a4a6, STORAGE_TYPE=MEMORY_CLUSTERED, JOB_NAME=ir.ivo.portal.core.portlet.MyTaskMessageListener}}
com.liferay.portal.kernel.messaging.MessageListenerException: java.lang.NullPointerException
    at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:32)
    at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:66)
    at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:74)
    at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:52)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:752)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:664)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at ir.ivo.portal.core.portlet.MyTaskMessageListener.doReceive(MyTaskMessageListener.java:88)
    at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:26)
    ... 6 more

Why is groupId here zero ?
And how can I fix my problem?

           
           
        
       
              
thumbnail
Mohammed yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi, 
   As your creating servicecontext object using ServiceContextThreadLocal.getServiceContext()  in message listener, it  is returning serviceContext without groupid and company id , you can create ServiceContext  object from new ServiceContext() constructor and set the group id and company id with setter.
 
  
thumbnail
ali yeganeh, modified 6 Years ago. Regular Member Posts: 148 Join Date: 5/1/19 Recent Posts
hi
I tried the way you said
But the group id generated by the code below is different from the group id of my database table
Company company = CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
long globalGroupId = company.getGroup().getGroupId();
thumbnail
Mohammed yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
long globalGroupId = company.getGroup().getGroupId();  this  is company  groupid, you might be looking for guest group id which you can get from  
GroupLocalServiceUtil.fetchGroup(company.getCompanyId, GroupConstants.GUEST) 
 
thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Hi Ali, Just to add a little more detail to help you understand why you are getting what you are getting (the empty values), the reason is because there is a servlet filter that is responsible for setting up the thread local. In your case, you have a scheduled task that is running on it's own, meaning it is not associated with a Request object that would have triggers the filter to setup the thread locals. So, as Mohammed has said, it's up to you to look up the values using the services. You can of course build out the service context yourself, it's just a bean after all. As an alternative to looking up the company by MX, if you know you are only working with the default portal instance, you could also try -
[code]
@Reference
private Portal _portal;
...
long companyId = _portal.getDefaultCompanyId()
...
thumbnail
ali yeganeh, modified 6 Years ago. Regular Member Posts: 148 Join Date: 5/1/19 Recent Posts
Hi
thank you very much for your answer
thumbnail
ali yeganeh, modified 6 Years ago. Regular Member Posts: 148 Join Date: 5/1/19 Recent Posts
Andrew Jardine:

 As an alternative to looking up the company by MX, if you know you are only working with the default portal instance, you could also try -
[code]
@Reference
private Portal _portal;
...
long companyId = _portal.getDefaultCompanyId()
...
Hi dear friend
Now what if I don't use the default instance?
and how can get liferay scope group Id ?
thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Well in that case I would probably use the VirtualHostLocalService to look up the VirtualHost record with a given domain, since the domain is supposed to be unique to a Portal Instance (Company). That Virtual Host record would then have the companyId value for the associated Company record. Note though, I was recently participating in some comments on a JIRA ticket for functionality that is going to allow for more than one domain to be associated with a company (an attempt to provide localization for domain records). That is in the works I believe. That won't affect you now, but it could be a breaking change in a future release. Just FYI.