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 @Deprecated
047 public SerialDestination(String name) {
048 super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
049 }
050
051 @Override
052 protected void dispatch(
053 final Set<MessageListener> messageListeners, final Message message) {
054
055 ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
056
057 Runnable runnable = new MessageRunnable(message) {
058
059 @Override
060 public void run() {
061 try {
062 populateThreadLocalsFromMessage(message);
063
064 for (MessageListener messageListener : messageListeners) {
065 try {
066 messageListener.receive(message);
067 }
068 catch (MessageListenerException mle) {
069 _log.error(
070 "Unable to process message " + message, mle);
071 }
072 }
073 }
074 finally {
075 ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
076
077 CentralizedThreadLocal.clearShortLivedThreadLocals();
078 }
079 }
080
081 };
082
083 threadPoolExecutor.execute(runnable);
084 }
085
086 private static final int _WORKERS_CORE_SIZE = 1;
087
088 private static final int _WORKERS_MAX_SIZE = 1;
089
090 private static final Log _log = LogFactoryUtil.getLog(
091 SerialDestination.class);
092
093 }