Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portal.messaging.proxy; import com.liferay.portal.kernel.messaging.Message; import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean; import com.liferay.portal.kernel.messaging.proxy.ProxyRequest; import com.liferay.portal.kernel.messaging.proxy.ProxyResponse; import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender; import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender; import com.liferay.util.aspectj.AspectJUtil; import java.util.Map; import org.aspectj.lang.ProceedingJoinPoint; /** * @author Michael C. Han * @author Brian Wing Shun Chan * @author Shuyang Zhou */ public class MessagingProxyAdvice { public Object invoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { Message message = new Message(); ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint); message.setPayload(proxyRequest); Map<String, Object> messageValues = MessageValuesThreadLocal.getValues(); if (!messageValues.isEmpty()) { for (String key : messageValues.keySet()) { message.put(key, messageValues.get(key)); } } BaseProxyBean baseProxyBean = (BaseProxyBean) proceedingJoinPoint.getTarget(); if (proxyRequest.isSynchronous() || ProxyModeThreadLocal.isForceSync()) { return doInvokeSynchronous(message, baseProxyBean); } else { doInvokeAsynchronous(message, baseProxyBean); return null; } } protected ProxyRequest createProxyRequest(ProceedingJoinPoint proceedingJoinPoint) throws Exception { return new ProxyRequest(AspectJUtil.getMethod(proceedingJoinPoint), proceedingJoinPoint.getArgs()); } protected void doInvokeAsynchronous(Message message, BaseProxyBean baseProxyBean) { SingleDestinationMessageSender messageSender = baseProxyBean.getSingleDestinationMessageSender(); if (messageSender == null) { throw new IllegalStateException("Asynchronous message sender was not configured properly for " + baseProxyBean.getClass().getName()); } messageSender.send(message); } protected Object doInvokeSynchronous(Message message, BaseProxyBean baseProxyBean) throws Exception { SingleDestinationSynchronousMessageSender messageSender = baseProxyBean .getSingleDestinationSynchronousMessageSender(); if (messageSender == null) { throw new IllegalStateException("Synchronous message sender was not configured properly for " + baseProxyBean.getClass().getName()); } ProxyResponse proxyResponse = (ProxyResponse) messageSender.send(message); if (proxyResponse == null) { return null; } else if (proxyResponse.hasError()) { throw proxyResponse.getException(); } else { return proxyResponse.getResult(); } } }