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            public void receive(
037                    RegistrationReference registrationReference, Datagram datagram) {
038    
039                    _executor.execute(new DispatchJob(registrationReference, datagram));
040            }
041    
042            protected abstract void doReceive(
043                            RegistrationReference registrationReference, Datagram datagram)
044                    throws Exception;
045    
046            private static Log _log = LogFactoryUtil.getLog(
047                    BaseAsyncDatagramReceiveHandler.class);
048    
049            private final Executor _executor;
050    
051            private class DispatchJob implements Runnable {
052    
053                    public DispatchJob(
054                            RegistrationReference registrationReference, Datagram datagram) {
055    
056                            _datagram = datagram;
057                            _registrationReference = registrationReference;
058                    }
059    
060                    public void run() {
061                            try {
062                                    doReceive(_registrationReference, _datagram);
063                            }
064                            catch (Exception e) {
065                                    _log.error("Unable to dispatch", e);
066                            }
067                    }
068    
069                    private final Datagram _datagram;
070                    private final RegistrationReference _registrationReference;
071    
072            }
073    
074    }