ddpush使用
梁华皓
2023-12-01
=======================================================TaskController
@Controller
@RequestMapping("patrol/task")
public class TaskController extends BaseController {
@Autowired
private WtXjServiceImpl wtxjserviceimpl;
/**
* 审核保存
*/
@RequestMapping(value = "save", method = RequestMethod.POST)
@ResponseBody
public String save(HttpServletRequest request) {
wtxjserviceimpl.saverecheck(request);
return "审核成功!";
}
}
======================================================WtXjServiceImpl
@Service
@Transactional(readOnly = true)
public class WtXjServiceImpl extends BaseService<TasklistEntity, Integer> {
@Autowired
private TipServiceImpl tipServiceImpl;
public void saverecheck(HttpServletRequest request) {
List<Hidderdanger> hidderdangers = yhpcDaoImpl.findHidderdanger(Integer
.parseInt(request.getParameter("sbid")));
for (Hidderdanger hidderdanger : hidderdangers) {
hidderdanger.setStatus(2);
hidderdangerDaoImpl.saveobj(hidderdanger);
Hidderdanger_recheck hr = new Hidderdanger_recheck();
hr.setHiddendangerid(hidderdanger.getId());
hr.setDangersourceid(1);
hr.setModifydepid(Integer.parseInt(request
.getParameter("modifydepid")));
if (request.getParameter("userid") != "") {
hr.setUserid(Integer.parseInt(request.getParameter("userid")));
} else {
hr.setUserid(1);// 目前是手动填写,用户id没有,这里暂时设置为1
}
if (request.getParameter("username") != "") {
hr.setUsername(request.getParameter("username"));
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
hr.setRectifytime(df.parse(request.getParameter("rectifytime")));
} catch (ParseException e) {
e.printStackTrace();
}
hr.setStatus(0);
dangerRecheckDaoImpl.saveobj(hr);
TipEntity tipEntity=new TipEntity();
tipEntity.setStatus(0);
tipEntity.setTipcontent("您有一条复查任务要执行");
tipEntity.setTipsourceid(hr.getId());
tipEntity.setTiptime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
tipEntity.setType(5);
tipEntity.setUserid(hr.getUserid());
tipServiceImpl.saveobj(tipEntity);
}
}
}
=============================================:TipServiceImpl
@Service
@Transactional(readOnly = true)
public class TipServiceImpl extends BaseService<TipEntity, Integer> {
public void saveobj(TipEntity tipEntity) {
tipDaoImpl.saveobj(tipEntity);
new Pushclass().sendMsg(String.valueOf(tipEntity.getUserid()),JsonMapper.getInstance().toJson(tipEntity));
System.out.println("===============:"+tipEntity.getUserid());
System.out.println("===============:"+JsonMapper.getInstance().toJson(tipEntity));
System.out.println("===============:"+tipEntity.getUserid());
}
}
===============================================工具
package com.xhtt.sys.comm.utils;
import org.ddpush.im.util.StringUtil;
import org.ddpush.im.v1.client.appserver.Pusher;
import org.ddpush.im.v1.client.appuser.Message;
import org.ddpush.im.v1.client.appuser.UDPClientBase;
public class Pushclass {
public void sendMsg(String toName, String whatYouWantSay) {
// 消息内容不能为空
if (whatYouWantSay.trim().equals("")) {
System.out.println("请输入你想要发送的内容");
}
// 从本地保存的设置读取相关消息
String serverIp = "192.168.1.30";
// new CommonClass().GetConfig("serverIp");
// new CommonClass().GetConfig("serverPort");
String pushPort = "9999";// new CommonClass().GetConfig("pushPort");
int port = 0;
try {
port = Integer.parseInt(pushPort);
} catch (Exception e) {
System.out.println("推送端口格式错误:" + pushPort);
}
byte[] uuid = null;
try {
uuid = StringUtil.md5Byte(toName.trim());
} catch (Exception e) {
System.out.println("错误:" + e.getMessage());
}
byte[] msg = null;
try {
msg = whatYouWantSay.getBytes("UTF-8");
} catch (Exception e) {
System.out.println("错误:" + e.getMessage());
}
Thread t = new Thread(new sendMsgTask(serverIp, port, uuid, msg));
t.start();
}
class sendMsgTask implements Runnable {
private String serverIp;
private int port;
private byte[] uuid;
private byte[] msg;
public sendMsgTask(String serverIp, int port, byte[] uuid, byte[] msg) {
this.serverIp = serverIp;
this.port = port;
this.uuid = uuid;
this.msg = msg;
}
public void run() {
Pusher pusher = null;
try {
boolean result;
pusher = new Pusher(serverIp, port, 1000 * 5);
result = pusher.push0x20Message(uuid, msg);
if (result) {
System.out.println("信息发送成功");
} else {
System.out.println("发送失败,格式有误");
}
} catch (Exception e) {
e.printStackTrace();// 网络连接异常
} finally {
if (pusher != null) {// 用完后,关闭pusher
try {
pusher.close();
} catch (Exception e) {
}
}
}
}
}
}