cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Parallel Streams with Hybris?

rdsa
Discoverer

i'm trying to use Java 8 parallel streams like so:

     bigList.parallelStream().forEach(item -> {
       foo();
     });

But I'm getting the following error:

 Caused by: java.lang.IllegalStateException: no tenant active. if you do not want to use tenants, call Registry.activateMasterTenant() to assure the Master tenant is active.

I did place this tenant check in the "foo" method:

     if (!Registry.hasCurrentTenant())
     {
         Registry.activateMasterTenant();
     }

but the error still happens.

Stacktrace:

 at de.hybris.platform.core.Registry.getCurrentTenant(Registry.java:823)
 at de.hybris.platform.util.Config.getBoolean(Config.java:245)
 at de.hybris.platform.util.persistence.PersistenceUtils.isPersistenceLagacyModeEnabledInConfig(PersistenceUtils.java:69)
 at de.hybris.platform.util.persistence.PersistenceUtils.isPersistenceLegacyModeEnabled(PersistenceUtils.java:64)
 at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.isSLDPersistence(DefaultModelService.java:477)
 at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.preloadItems(DefaultModelService.java:467)
 at de.hybris.platform.servicelayer.search.internal.resolver.impl.DefaultModelResolver.preloadItems(DefaultModelResolver.java:95)
 at de.hybris.platform.servicelayer.search.impl.LazyLoadModelList.loadPage(LazyLoadModelList.java:62)
 at de.hybris.platform.core.LazyLoadItemList.switchPage(LazyLoadItemList.java:217)
 at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageNoLock(LazyLoadItemList.java:473)
 at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageSynchronized(LazyLoadItemList.java:465)
 at de.hybris.platform.core.LazyLoadItemList.switchBufferedPage(LazyLoadItemList.java:460)
 at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:451)
 at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:431)
 at de.hybris.platform.core.LazyLoadItemList.getBuffered(LazyLoadItemList.java:109)
 at de.hybris.platform.core.LazyLoadItemList.get(LazyLoadItemList.java:95)
 at java.util.AbstractList$Itr.next(AbstractList.java:358)
 at de.hybris.platform.core.internal.BaseLazyLoadItemList$1.next(BaseLazyLoadItemList.java:177)
 at java.util.Spliterators$IteratorSpliterator.trySplit(Spliterators.java:1784)
 at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
 at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
 at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
 at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
 at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
 at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

Any ideas?

Accepted Solutions (1)

Accepted Solutions (1)

former_member620692
Active Contributor
0 Kudos

The processing under parallel stream uses the ForkJoinPool.commonPool() but it does not have any tenant set to it. You need to create a custom thread pool, set tenant to every thread of the thread pool and then use the thread pool to process the parallel stream. You can check https://www.baeldung.com/java-8-parallel-streams-custom-threadpool for an example of how to use a custom thread pool with parallel stream.

Answers (0)