Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: create service context in component class
Hello
This is my sample code in the Message Listener field
I had the following error
Thanks for helping guide me
Why is groupId here zero ?
And how can I fix my problem?
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?
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.
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.
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
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();
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)
GroupLocalServiceUtil.fetchGroup(company.getCompanyId, GroupConstants.GUEST)
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()
...
Hi
thank you very much for your answer
thank you very much for your answer
Andrew Jardine:
Hi dear friend
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()
...
Now what if I don't use the default instance?
and how can get liferay scope group Id ?
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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™