001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.nio.intraband;
016    
017    import com.liferay.portal.kernel.executor.PortalExecutorManagerUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import java.util.concurrent.Executor;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public abstract class BaseAsyncDatagramReceiveHandler
027            implements DatagramReceiveHandler {
028    
029            public BaseAsyncDatagramReceiveHandler() {
030                    Class<? extends BaseAsyncDatagramReceiveHandler> clazz = getClass();
031    
032                    _executor = PortalExecutorManagerUtil.getPortalExecutor(
033                            clazz.getName());
034            }
035    
036            @Override
037            public void receive(
038                    RegistrationReference registrationReference, Datagram datagram) {
039    
040                    _executor.execute(new DispatchJob(registrationReference, datagram));
041            }
042    
043            protected abstract void doReceive(
044                            RegistrationReference registrationReference, Datagram datagram)
045                    throws Exception;
046    
047            private static Log _log = LogFactoryUtil.getLog(
048                    BaseAsyncDatagramReceiveHandler.class);
049    
050            private final Executor _executor;
051    
052            private class DispatchJob implements Runnable {
053    
054                    public DispatchJob(
055                            RegistrationReference registrationReference, Datagram datagram) {
056    
057                            _datagram = datagram;
058                            _registrationReference = registrationReference;
059                    }
060    
061                    @Override
062                    public void run() {
063                            try {
064                                    doReceive(_registrationReference, _datagram);
065                            }
066                            catch (Exception e) {
067                                    _log.error("Unable to dispatch", e);
068                            }
069                    }
070    
071                    private final Datagram _datagram;
072                    private final RegistrationReference _registrationReference;
073    
074            }
075    
076    }