Java tutorial
/** * Copyright (C) 2016 Newland Group Holding Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.avatarmq.broker; import com.newlandframework.avatarmq.core.CallBackInvoker; import com.newlandframework.avatarmq.core.MessageSystemConfig; import com.newlandframework.avatarmq.model.ResponseMessage; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.TimeUnit; /** * @filename:SendMessageLauncher.java * @description:SendMessageLauncher? * @author tangjie<https://github.com/tang-jie> * @blog http://www.cnblogs.com/jietang/ * @since 2016-8-11 */ public class SendMessageLauncher { private long timeout = MessageSystemConfig.MessageTimeOutValue; public Map<String, CallBackInvoker<Object>> invokeMap = new ConcurrentSkipListMap<String, CallBackInvoker<Object>>(); private SendMessageLauncher() { } private static SendMessageLauncher resource; public static SendMessageLauncher getInstance() { if (resource == null) { synchronized (SendMessageLauncher.class) { if (resource == null) { resource = new SendMessageLauncher(); } } } return resource; } public Object launcher(Channel channel, ResponseMessage response) { if (channel != null) { CallBackInvoker<Object> invoke = new CallBackInvoker<Object>(); invokeMap.put(response.getMsgId(), invoke); invoke.setRequestId(response.getMsgId()); ChannelFuture channelFuture = channel.writeAndFlush(response); channelFuture.addListener(new LauncherListener(invoke)); try { Object result = invoke.getMessageResult(timeout, TimeUnit.MILLISECONDS); return result; } catch (RuntimeException e) { throw e; } finally { invokeMap.remove(response.getMsgId()); } } else { return null; } } public boolean trace(String key) { return invokeMap.containsKey(key); } public CallBackInvoker<Object> detach(String key) { if (invokeMap.containsKey(key)) { return invokeMap.remove(key); } else { return null; } } }