创建XmppMessageUtil类
static Logger logger = LoggerFactory.getLogger(XmppMessageUtil.class);
private static XmppMessageUtil xmppMessageUtil;
@Resource(name = "loadBalanced")
private RestTemplate loadBalanced;
使用
@PostConstruct注解(
@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。
被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行
)
public static void sendXmppMessage(String userFromId, String password, String userId, XmppMessageBody body) {
try {
body.setTimeSend(new Date());
Map<String, Object> mapPara = new HashMap<String, Object>();
mapPara.put("userId", userId);
mapPara.put("body", JSON.toJSONString(body));
mapPara.put("userFromId", userFromId);
mapPara.put("password", password);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
com.yhyt.health.spring.AppResult result = xmppMessageUtil.loadBalanced.postForObject("http://dialog/dialog/sendMessage?userId={userId}&body={body}", httpEntity, com.yhyt.health.spring.AppResult.class, mapPara);
if (null != result && "200".equals(result.getStatus().getCode())) {
logger.info("推送下线成功");
}
} catch (Exception e) {
logger.error("发送xmpp失败,用户id"+userId,e);
}
}