001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }