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.netty; import com.newlandframework.avatarmq.msg.ProducerAckMessage; import com.newlandframework.avatarmq.core.CallBackInvoker; import com.newlandframework.avatarmq.core.CallBackListener; import com.newlandframework.avatarmq.core.NotifyCallback; import com.newlandframework.avatarmq.model.RequestMessage; import com.newlandframework.avatarmq.model.ResponseMessage; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /** * @filename:MessageProcessor.java * @description:MessageProcessor? * @author tangjie<https://github.com/tang-jie> * @blog http://www.cnblogs.com/jietang/ * @since 2016-8-11 */ public class MessageProcessor { private MessageConnectFactory factory = null; private MessageConnectPool pool = null; public MessageProcessor(String serverAddress) { MessageConnectPool.setServerAddress(serverAddress); pool = MessageConnectPool.getMessageConnectPoolInstance(); this.factory = pool.borrow(); } public void closeMessageConnectFactory() { pool.restore(); } public MessageConnectFactory getMessageConnectFactory() { return factory; } public void sendAsynMessage(RequestMessage request, final NotifyCallback listener) { Channel channel = factory.getMessageChannel(); if (channel == null) { return; } Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); invoker.join(new CallBackListener<Object>() { public void onCallBack(Object t) { ResponseMessage response = (ResponseMessage) t; listener.onEvent((ProducerAckMessage) response.getMsgParams()); } }); ChannelFuture channelFuture = channel.writeAndFlush(request); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); } public Object sendAsynMessage(RequestMessage request) { Channel channel = factory.getMessageChannel(); if (channel == null) { return null; } Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); ChannelFuture channelFuture = channel.writeAndFlush(request); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); try { Object result = invoker.getMessageResult(factory.getTimeOut(), TimeUnit.MILLISECONDS); callBackMap.remove(request.getMsgId()); return result; } catch (RuntimeException e) { e.printStackTrace(); return null; } } public void sendSyncMessage(RequestMessage request) { Channel channel = factory.getMessageChannel(); if (channel == null) { return; } Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); ChannelFuture channelFuture; try { channelFuture = channel.writeAndFlush(request).sync(); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); } catch (InterruptedException ex) { Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, null, ex); } } }