View Javadoc
1   package com.foxinmy.weixin4j.server.test;
2   
3   import io.netty.channel.ChannelHandlerContext;
4   
5   import org.springframework.context.ApplicationContext;
6   
7   import com.foxinmy.weixin4j.dispatcher.BeanFactory;
8   import com.foxinmy.weixin4j.handler.DebugMessageHandler;
9   import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
10  import com.foxinmy.weixin4j.handler.MultipleMessageHandlerAdapter;
11  import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
12  import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
13  import com.foxinmy.weixin4j.message.TextMessage;
14  import com.foxinmy.weixin4j.message.VoiceMessage;
15  import com.foxinmy.weixin4j.mp.event.ScanEventMessage;
16  import com.foxinmy.weixin4j.request.WeixinMessage;
17  import com.foxinmy.weixin4j.request.WeixinRequest;
18  import com.foxinmy.weixin4j.response.TextResponse;
19  import com.foxinmy.weixin4j.response.WeixinResponse;
20  import com.foxinmy.weixin4j.spring.SpringBeanFactory;
21  import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
22  
23  /**
24   * 服务启动测试类
25   *
26   * @className MessageServerStartup
27   * @author jinyu(foxinmy@gmail.com)
28   * @date 2015年5月7日
29   * @since JDK 1.6
30   * @see
31   */
32  public class MessageServerStartup {
33  
34      // 公众号ID
35      final String weixinId = "wx4ab8f8de58159a57";
36      // 开发者token
37      final String token = "weixin4j";
38      // AES密钥(安全模式)
39      final String aesKey = "";
40  
41      /**
42       * 调试输出用户发来的消息
43       *
44       */
45      public void test1() {
46          // 明文模式
47          new WeixinServerBootstrap(token).addHandler(DebugMessageHandler.global).startup();
48          // 密文模式
49          // new WeixinServerBootstrap(weixinId, token, aesKey).addHandler(
50          // DebugMessageHandler.global).startup();
51      }
52  
53      /**
54       * 针对特定消息类型
55       *
56       */
57      public void test2() {
58          // 针对文本消息回复
59          WeixinMessageHandler textMessageHandler = new MessageHandlerAdapter<TextMessage>() {
60              @Override
61              protected WeixinResponse doHandle0(TextMessage message) {
62                  return new TextResponse("HelloWorld!");
63              }
64          };
65          // 针对语音消息回复
66          WeixinMessageHandler voiceMessageHandler = new MessageHandlerAdapter<VoiceMessage>() {
67              @Override
68              protected WeixinResponse doHandle0(VoiceMessage message) {
69                  return new TextResponse("HelloWorld!");
70              }
71          };
72          // 当消息类型为文本(text)或者语音时回复「HelloWorld」, 否则回复调试消息
73          new WeixinServerBootstrap(weixinId, token, aesKey)
74                  .addHandler(textMessageHandler, voiceMessageHandler, DebugMessageHandler.global).startup();
75      }
76  
77      /**
78       * 多种消息类型处理
79       *
80       */
81      public void test3() {
82          @SuppressWarnings("unchecked")
83          MultipleMessageHandlerAdapter messageHandler = new MultipleMessageHandlerAdapter(ScanEventMessage.class,
84                  TextMessage.class) {
85  			@Override
86  			public WeixinResponse doHandle(WeixinRequest request,
87  					WeixinMessage message) {
88  				 return new TextResponse("处理了扫描和文字消息");
89  			}
90          };
91          new WeixinServerBootstrap(token).addHandler(messageHandler, DebugMessageHandler.global).startup();
92      }
93  
94      /**
95       * 扫描包添加handler
96       *
97       * @
98       */
99      public void test4() {
100         // handler处理所在的包名(子包也会扫描)
101         String packageToScan = "com.foxinmy.weixin4j.handler";
102         // handler默认使用 Class.newInstance
103         // 方式实例化,如果handler中含有service等类需要注入,可以声明一个BeanFactory,如SpringBeanFactory
104         ApplicationContext applicationContext = null; // spring容器
105         BeanFactory beanFactory = new SpringBeanFactory(applicationContext);
106         new WeixinServerBootstrap(token).handlerPackagesToScan(packageToScan).openAlwaysResponse()
107                 .resolveBeanFactory(beanFactory).startup();
108     }
109 
110     /**
111      * 拦截器应用
112      *
113      * @
114      */
115     public void test5() {
116         // 拦截所有请求
117         WeixinMessageInterceptor interceptor = new WeixinMessageInterceptor() {
118             @Override
119             public boolean preHandle(ChannelHandlerContext context, WeixinRequest request, WeixinMessage message,
120                     WeixinMessageHandler handler) {
121                 context.writeAndFlush(new TextResponse("所有消息被拦截了!"));
122                 return false;
123             }
124 
125             @Override
126             public void postHandle(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response,
127                     WeixinMessage message, WeixinMessageHandler handler) {
128                 System.err.println("preHandle返回为true,执行handler后");
129             }
130 
131             @Override
132             public void afterCompletion(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response,
133                     WeixinMessage message, WeixinMessageHandler handler, Exception exception) {
134                 System.err.println("请求处理完毕");
135             }
136 
137             @Override
138             public int weight() {
139                 return 0;
140             }
141         };
142         new WeixinServerBootstrap(token).addInterceptor(interceptor).openAlwaysResponse().startup();
143     }
144 
145     /**
146      * main方法入口
147      *
148      * @param args
149      * @throws Exception
150      */
151     public static void main(String[] args) throws Exception {
152         new MessageServerStartup().test1();
153     }
154 }