Commit 43db8748 authored by Paul0523's avatar Paul0523

增加缓存消息功能

parent 9f71afa9
package com.yg84;
import com.yg84.weixin.Contact;
import com.yg84.weixin.Message;
import com.yg84.weixin.MessageHandler;
import com.yg84.weixin.WeChat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
/**
* Created by fangzhipeng on 2017/7/16.
......@@ -12,18 +18,43 @@ import java.util.List;
@RestController
public class Login {
public static WeChat weChat;
ArrayBlockingQueue<Message> messageQueue = new ArrayBlockingQueue<Message>(10000);
@PostConstruct
public void initWeChat() {
MessageHandler handler = new MessageHandler() {
@Override
public void handleMsg(List<Message> messages) throws Exception {
messageQueue.addAll(messages);
System.out.println("成功加入" + messages.size() + "条信息");
}
};
weChat = new WeChat(handler);
}
@RequestMapping(value = "/login")
public String login() throws Exception {
return WeixinApplication.weChat.run();
return weChat.run();
}
@RequestMapping(value = "/sendMsg")
public String sendMsg(String name, String content) throws Exception{
return WeixinApplication.weChat.sendMsg(name, content);
return weChat.sendMsg(name, content);
}
@RequestMapping(value = "/getContact")
public List<Contact> getContact() {
return WeixinApplication.weChat.getContacts();
return weChat.getContacts();
}
@RequestMapping(value = "/loadMessage")
public List<Message> loadMessage() {
List<Message> messages = new ArrayList<>();
Message message;
while ((message = messageQueue.poll()) != null) {
messages.add(message);
}
return messages;
}
}
......@@ -10,7 +10,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class WeixinApplication {
public static WeChat weChat = new WeChat(null);
public static void main(String[] args) {
System.setProperty("jsse.enableSNIExtension", "false");
......
......@@ -5,9 +5,18 @@ package com.yg84.weixin;
*/
public class Message {
private Integer msgType;
private String fromUserName;
private String content;
public Integer getMsgType() {
return msgType;
}
public void setMsgType(Integer msgType) {
this.msgType = msgType;
}
public String getFromUserName() {
return fromUserName;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment