cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to register my custom thread (after creating thru Thread t; t.start()) in hybris

Former Member
0 Kudos
3,069

Hi Guys, Can anyone help me to know- how can I register my custom thread created in hybris.

My code sample:

final Runnable runnable = () -> { // my code here.. }; final Thread t = new Thread(runnable); t.start();

If I use t.run() in place of t.start() then my code inside the runnable is getting executed but if I use t.start() then code inside the runnable is not getting executed and I am getting error saying:

Exception in thread "Thread-134" java.lang.IllegalStateException: no tenant active. if you do not want to use tenants, call Registry.activateMasterTenant() to assure the Master tenant is active. INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.core.Registry.getCurrentTenant(Registry.java:804) at de.hybris.platform.util.Config.getBoolean(Config.java:245) INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.util.persistence.PersistenceUtils.isPersistenceLagacyModeEnabledInConfig(PersistenceUtils.java:68) INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.util.persistence.PersistenceUtils.isPersistenceLegacyModeEnabled(PersistenceUtils.java:63) INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.isSLDPersistence(DefaultModelService.java:476) INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.preloadItems(DefaultModelService.java:466) INFO | jvm 1 | main | 2018/10/01 15:35:52.038 | at de.hybris.platform.servicelayer.search.internal.resolver.impl.DefaultModelResolver.preloadItems(DefaultModelResolver.java:95) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.servicelayer.search.impl.LazyLoadModelList.loadPage(LazyLoadModelList.java:62) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.switchPage(LazyLoadItemList.java:216) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageNoLock(LazyLoadItemList.java:472) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageSynchronized(LazyLoadItemList.java:464) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.switchBufferedPage(LazyLoadItemList.java:459) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:450) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:430) INFO | jvm 1 | main | 2018/10/01 15:35:52.039 | at de.hybris.platform.core.LazyLoadItemList.getBuffered(LazyLoadItemList.java:108) INFO | jvm 1 | main | 2018/10/01 15:35:52.051 | at de.hybris.platform.core.LazyLoadItemList.get(LazyLoadItemList.java:94) INFO | jvm 1 | main | 2018/10/01 15:35:52.053 | at java.util.AbstractList$Itr.next(AbstractList.java:358) INFO | jvm 1 | main | 2018/10/01 15:35:52.053 | at de.hybris.platform.core.internal.BaseLazyLoadItemList$1.next(BaseLazyLoadItemList.java:177) INFO | jvm 1 | main | 2018/10/01 15:35:52.053 | at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042) INFO | jvm 1 | main | 2018/10/01 15:35:52.053 | at com.carrefour.core.cronjob.CarrefourAPICachCronJob.lambda$0(CarrefourAPICachCronJob.java:76) INFO | jvm 1 | main | 2018/10/01 15:35:52.053 | at java.lang.Thread.run(Thread.java:748)

I checked for the above issue in my local, and found that MASTER tenant is ACTIVE. So this error is not relative to my code/requirement. If we use t.run() then we are using thread from hybris itself which I don't want. I want to create my own thread and then I want to execute my code with that thread. So to do this, I need to register my thread into hybris then only hybris will recognize my thread as well in the system.

Any small clue is APPRECIATED. Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

mpern
Product and Topic Expert
Product and Topic Expert

To get a properly initialized thread, use de.hybris.platform.core.TenantAwareThreadFactory

Depending on your hybris version, there should be a bean with ID tenantAwareThreadFactory available that you can use to get threads.

If you don't have such a bean, you can easily define it yourself:

 <bean id="myThreadFactor" class="de.hybris.platform.core.TenantAwareThreadFactory">
     <constructor-arg name="tenant" ref="tenantFactory"/>
 </bean>

And a recommendation:

Do not use concurrency primitives like Thread etc directly!

Read "Java Concurrency in Practice" by Brian Goetz, and then build your stuff with the tools available in the java.util.concurrent package, e.g. ExecutorService. It really, really makes your life easier.

Answers (1)

Answers (1)

arvind-kumar_avinash
Active Contributor