Search in sources :

Example 1 with WxCpXmlMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouterTest method testConcurrency.

public void testConcurrency() throws InterruptedException {
    final WxCpMessageRouter router = new WxCpMessageRouter(null);
    router.rule().handler(new WxCpMessageHandler() {

        @Override
        public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) {
            return null;
        }
    }).end();
    final WxCpXmlMessage m = new WxCpXmlMessage();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            router.route(m);
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
            }
        }
    };
    for (int i = 0; i < 10; i++) {
        new Thread(r).start();
    }
    Thread.sleep(1000l * 2);
}
Also used : WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) WxSessionManager(me.chanjar.weixin.common.session.WxSessionManager) Map(java.util.Map)

Example 2 with WxCpXmlMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlMessage in project weixin-java-tools by chanjarster.

the class WxCpDemoServer method initWeixin.

private static void initWeixin() {
    InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
    WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1);
    wxCpConfigStorage = config;
    wxCpService = new WxCpServiceImpl();
    wxCpService.setWxCpConfigStorage(config);
    WxCpMessageHandler handler = new WxCpMessageHandler() {

        @Override
        public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) {
            WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName()).build();
            return m;
        }
    };
    WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {

        @Override
        public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) {
            String href = "<a href=\"" + wxCpService.oauth2buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null) + "\">测试oauth2</a>";
            return WxCpXmlOutMessage.TEXT().content(href).fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName()).build();
        }
    };
    wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
    wxCpMessageRouter.rule().async(false).content(// 拦截内容为“哈哈”的消息
    "哈哈").handler(handler).end().rule().async(false).content("oauth").handler(oauth2handler).end();
}
Also used : WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) InputStream(java.io.InputStream) WxSessionManager(me.chanjar.weixin.common.session.WxSessionManager) WxCpXmlOutTextMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage) Map(java.util.Map)

Example 3 with WxCpXmlMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouterTest method testSessionClean1.

@Test(dataProvider = "standardSessionManager")
public void testSessionClean1(StandardSessionManager ism) throws InterruptedException {
    // 两个同步请求,看是否处理完毕后会被清理掉
    final WxCpMessageRouter router = new WxCpMessageRouter(null);
    router.setSessionManager(ism);
    router.rule().async(false).handler(new WxSessionMessageHandler()).next().rule().async(false).handler(new WxSessionMessageHandler()).end();
    WxCpXmlMessage msg = new WxCpXmlMessage();
    msg.setFromUserName("abc");
    router.route(msg);
    Thread.sleep(2000l);
    Assert.assertEquals(ism.getActiveSessions(), 0);
}
Also used : WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) Test(org.testng.annotations.Test)

Example 4 with WxCpXmlMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouterTest method testSessionClean3.

@Test(dataProvider = "standardSessionManager")
public void testSessionClean3(StandardSessionManager ism) throws InterruptedException {
    // 2个异步请求,看是否处理完毕后会被清理掉
    final WxCpMessageRouter router = new WxCpMessageRouter(null);
    router.setSessionManager(ism);
    router.rule().async(true).handler(new WxSessionMessageHandler()).next().rule().async(true).handler(new WxSessionMessageHandler()).end();
    WxCpXmlMessage msg = new WxCpXmlMessage();
    msg.setFromUserName("abc");
    router.route(msg);
    Thread.sleep(2000l);
    Assert.assertEquals(ism.getActiveSessions(), 0);
}
Also used : WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) Test(org.testng.annotations.Test)

Example 5 with WxCpXmlMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouterTest method messages2.

@DataProvider(name = "messages-1")
public Object[][] messages2() {
    WxCpXmlMessage message1 = new WxCpXmlMessage();
    message1.setMsgType(WxConsts.XML_MSG_TEXT);
    WxCpXmlMessage message2 = new WxCpXmlMessage();
    message2.setEvent(WxConsts.EVT_CLICK);
    WxCpXmlMessage message3 = new WxCpXmlMessage();
    message3.setEventKey("KEY_1");
    WxCpXmlMessage message4 = new WxCpXmlMessage();
    message4.setContent("CONTENT_1");
    WxCpXmlMessage message5 = new WxCpXmlMessage();
    message5.setContent("BLA");
    WxCpXmlMessage message6 = new WxCpXmlMessage();
    message6.setContent("abcd");
    WxCpXmlMessage message7 = new WxCpXmlMessage();
    message7.setFormat("strangeformat");
    WxCpXmlMessage c2 = new WxCpXmlMessage();
    c2.setMsgType(WxConsts.XML_MSG_TEXT);
    c2.setEvent(WxConsts.EVT_CLICK);
    WxCpXmlMessage c3 = new WxCpXmlMessage();
    c3.setMsgType(WxConsts.XML_MSG_TEXT);
    c3.setEvent(WxConsts.EVT_CLICK);
    c3.setEventKey("KEY_1");
    WxCpXmlMessage c4 = new WxCpXmlMessage();
    c4.setMsgType(WxConsts.XML_MSG_TEXT);
    c4.setEvent(WxConsts.EVT_CLICK);
    c4.setEventKey("KEY_1");
    c4.setContent("CONTENT_1");
    return new Object[][] { new Object[] { message1, WxConsts.XML_MSG_TEXT + "," }, new Object[] { message2, WxConsts.EVT_CLICK + "," }, new Object[] { message3, "KEY_1," }, new Object[] { message4, "CONTENT_1," }, new Object[] { message5, "ALL," }, new Object[] { message6, "abcd," }, new Object[] { message7, "matcher," }, new Object[] { c2, "COMBINE_2," }, new Object[] { c3, "COMBINE_3," }, new Object[] { c4, "COMBINE_4," } };
}
Also used : WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) DataProvider(org.testng.annotations.DataProvider)

Aggregations

WxCpXmlMessage (me.chanjar.weixin.cp.bean.WxCpXmlMessage)6 Map (java.util.Map)2 WxSessionManager (me.chanjar.weixin.common.session.WxSessionManager)2 Test (org.testng.annotations.Test)2 InputStream (java.io.InputStream)1 WxCpXmlOutMessage (me.chanjar.weixin.cp.bean.WxCpXmlOutMessage)1 WxCpXmlOutTextMessage (me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage)1 WxCpCryptUtil (me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil)1 DataProvider (org.testng.annotations.DataProvider)1