001
014
015 package com.liferay.portal.kernel.messaging;
016
017 import com.liferay.portal.kernel.cache.Lifecycle;
018 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
019 import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.CentralizedThreadLocal;
023
024 import java.util.Set;
025
026
034 public class SerialDestination extends BaseAsyncDestination {
035
036 public SerialDestination() {
037 super();
038
039 setWorkersCoreSize(_WORKERS_CORE_SIZE);
040 setWorkersMaxSize(_WORKERS_MAX_SIZE);
041 }
042
043
046 public SerialDestination(String name) {
047 super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
048 }
049
050 @Override
051 protected void dispatch(
052 final Set<MessageListener> messageListeners, final Message message) {
053
054 ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
055
056 Runnable runnable = new MessageRunnable(message) {
057
058 @Override
059 public void run() {
060 try {
061 populateThreadLocalsFromMessage(message);
062
063 for (MessageListener messageListener : messageListeners) {
064 try {
065 messageListener.receive(message);
066 }
067 catch (MessageListenerException mle) {
068 _log.error(
069 "Unable to process message " + message, mle);
070 }
071 }
072 }
073 finally {
074 ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
075
076 CentralizedThreadLocal.clearShortLivedThreadLocals();
077 }
078 }
079
080 };
081
082 threadPoolExecutor.execute(runnable);
083 }
084
085 private static final int _WORKERS_CORE_SIZE = 1;
086
087 private static final int _WORKERS_MAX_SIZE = 1;
088
089 private static Log _log = LogFactoryUtil.getLog(SerialDestination.class);
090
091 }