最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java定时任务使用方法之实现环信聊天记录同步并定时删除
时间:2016-08-22 编辑:简简单单 来源:一聚教程网
定时任务代码:
public class ChatHistoryTimerListener implements ServletContextListener {
String chatUrl="http://a1.easemob.com/***/********/chatmessages";
private ChatHistoryService chService;
private Timer timer=null;
private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
//private static final long PERIOD_DAY = 1000;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
timer=new Timer();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
this.chService=(ChatHistoryService)applicationContext.getBean("chService");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 1); //凌晨1点
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND, 0);
Date date=calendar.getTime();
event.getServletContext().log("环信----------------定时器已启动。") ;
TimerTask task=new TimerTask() {
@Override
public void run() {
String tokenUrl="http://a1.easemob.com/***/*****/token";
Map
paramMap.put("grant_type", "client_credentials");
paramMap.put("client_id", Config.hxclient_id);
paramMap.put("client_secret", Config.hxclient_secret);
Map
System.out.println(calendar.get(Calendar.MINUTE));
List
private void deleteFile(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i=0; i
}
}
file.delete();
}
}
业务代码
public void SynchronousChat(JSONObject o, String filepath) {
JSONArray e = (JSONArray) o.get("entities");
if (e.size() > 0) {
List
for (int i = 0; i < e.size(); i++) {
B_Chat_History bcHistory = new B_Chat_History();
JSONObject s = e.getJSONObject(i);
String from = s.getString("from");
String to = s.getString("to");
Long timestamp = Long.parseLong(s.getString("timestamp"));
JSONObject p = (JSONObject) s.get("payload");
JSONArray bodies = (JSONArray) p.get("bodies");
JSONObject b = bodies.getJSONObject(0);
// "msg": "哈哈哈","type":
// "txt""type": "video",
// 开始赋值
bcHistory.setFrom_phone(from);
bcHistory.setTo_phone(to);
bcHistory.setFrom_user(getuserName(from));
bcHistory.setTo_user(getuserName(to));
bcHistory.setTimestamp(new java.sql.Timestamp(timestamp));
switch (b.getString("type")) {
case "txt":
bcHistory.setMsg(b.getString("msg").replace("\n", ""));
bcHistory.setType("txt");
bcHistory.setLength(0);
break;
case "video":
bcHistory.setType("video");
String videokey = "video_" + new Date().getTime() + ".mp4";
try {
String file = HttpDownUtil.downloadNet(filepath,
b.getString("url"), "mp4");
try {
QiNiuUplodUtil.upload(file, videokey);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
bcHistory.setUrl(Constant.QINIU_URL_WEB + videokey);
String thumbkey = "video_" + new Date().getTime() + ".png";
try {
String file = HttpDownUtil.downloadNet(filepath,
b.getString("thumb"), "png");
try {
QiNiuUplodUtil.upload(file, thumbkey);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
bcHistory.setThumb(Constant.QINIU_URL_WEB + thumbkey);
bcHistory.setLength(0);
break;
case "img":
bcHistory.setType("img");
String imgkey = "img_" + new Date().getTime() + ".png";
try {
String file = HttpDownUtil.downloadNet(filepath,
b.getString("url"), "png");
try {
QiNiuUplodUtil.upload(file, imgkey);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
bcHistory.setUrl(Constant.QINIU_URL_WEB + imgkey);
bcHistory.setLength(0);
break;
case "audio":
bcHistory.setType("audio");
String audiokey = "audio_" + new Date().getTime() + ".amr";
try {
String file = HttpDownUtil.downloadNet(filepath,
b.getString("url"), "amr");
try {
QiNiuUplodUtil.upload(file, audiokey);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
//"length":10,语音时常秒
bcHistory.setLength(Integer.parseInt(b.getString("length")));
bcHistory.setUrl(Constant.QINIU_URL_WEB + audiokey);
break;
}
list.add(bcHistory);
}
/**
* 此处使用Springjdbc执行批量保存
*/
try {
jt.batchUpdate(
"insert into b_chat_history (id,from_phone,from_user,to_phone,to_user,type,msg,url,thumb,length,timestamp) value (?,?,?,?,?,?,?,?,?,?,?)",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setString(1, UUID.randomUUID().toString());// ID的值
ps.setString(2, list.get(i).getFrom_phone());
ps.setString(3, list.get(i).getFrom_user());
ps.setString(4, list.get(i).getTo_phone());
ps.setString(5, list.get(i).getTo_user());
ps.setString(6, list.get(i).getType());
ps.setString(7, list.get(i).getMsg());
ps.setString(8, list.get(i).getUrl());
ps.setString(9, list.get(i).getThumb());
ps.setInt(10,list.get(i).getLength());
ps.setTimestamp(11, list.get(i).getTimestamp());
}
public int getBatchSize() {
return list.size();
}
});
} catch (Exception e2) {
System.out.println("可能有数据异常,同步部分数据异常");
e2.printStackTrace();
}
}
}
OK了,最后实现的结果如下
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24