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.core.HookMessageEvent; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.NameMatchMethodPointcutAdvisor; /** * @filename:MessageEventWrapper.java * @description:MessageEventWrapper? * @author tangjie<https://github.com/tang-jie> * @blog http://www.cnblogs.com/jietang/ * @since 2016-8-11 */ public class MessageEventWrapper<T> extends ChannelInboundHandlerAdapter implements MessageEventHandler, MessageEventProxy { final public static String proxyMappedName = "handleMessage"; protected MessageProcessor processor; protected Throwable cause; protected HookMessageEvent<T> hook; protected MessageConnectFactory factory; private MessageEventWrapper<T> wrapper; public MessageEventWrapper() { } public MessageEventWrapper(MessageProcessor processor) { this(processor, null); } public MessageEventWrapper(MessageProcessor processor, HookMessageEvent<T> hook) { this.processor = processor; this.hook = hook; this.factory = processor.getMessageConnectFactory(); } public void handleMessage(ChannelHandlerContext ctx, Object msg) { return; } public void beforeMessage(Object msg) { } public void afterMessage(Object msg) { } public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); } public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); ProxyFactory weaver = new ProxyFactory(wrapper); NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(); advisor.setMappedName(MessageEventWrapper.proxyMappedName); advisor.setAdvice(new MessageEventAdvisor(wrapper, msg)); weaver.addAdvisor(advisor); MessageEventHandler proxyObject = (MessageEventHandler) weaver.getProxy(); proxyObject.handleMessage(ctx, msg); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { this.cause = cause; cause.printStackTrace(); } public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); } public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { super.channelReadComplete(ctx); } public Throwable getCause() { return cause; } public void setWrapper(MessageEventWrapper<T> wrapper) { this.wrapper = wrapper; } }