在BOS开发中如果需要往消息中心发送消息(例如预警功能),代码案例如下:
可参考EvalWorkFlowFacadeControllerBean中_sendMessage(方法)
主要有两种:通知消息 和即时消息
1.通知消息的实现
private static void sendMessage(Context ctx, String remindPersonId, String title, String sendMessage){
try{
SolutionInfo solution = MetaDataLoaderFactory.getLocalMetaDataLoader(ctx).getSolution();
SenderAgent sender = SenderAgent.getSenderAgent();
LanguageCollection language = null;
if(solution != null){
language = solution.getLanguages();
}
Message message = MessageFactory.newMessage("kingdee.workflow");
if(language.size()>0){
for(int i=0; i<language.size(); i++){
message.setLocaleStringHeader("title", title, language.get(i).getLocale());
message.setLocaleStringHeader("sender", ContextHelperFactory.getLocalInstance(ctx).getCurrentUser().getName(), language.get(i).getLocale());
message.setLocaleStringHeader("body", sendMessage, language.get(i).getLocale());
}
message.setIntHeader("type", MsgType.NOTICE_VALUE);//设置消息类型为通知
message.setIntHeader("bizType", MsgBizType.FORWARN_VALUE);//业务类型设置为工作流
message.setIntHeader("sourceStatus", MsgSourceStatus.EMPTY_VALUE);//设置任务状态,此处是通知消息,所以设置空
message.setIntHeader("priority", MsgPriority.MIDDLE_VALUE);//设置消息优先级,自己根据需要设定相应的级别
message.setStringHeader("databaseCenter", ctx.getAIS());//得到数据中心
message.setStringHeader("solution", ctx.getSolution());//设置解决方案
message.setStringHeader("receiver",remindPersonId);//设置接收用户ID
sender.sendMessage(message);//发送消息
}
}catch(Exception e){
e.printStackTrace();
}
}
2.即时消息的实现(可实习消息框提示起到即时预警作用)
BMCMessageInfo msgInfo=new BMCMessageInfo();
BasReleaseInfo receiverInfo=new BasReleaseInfo();
java.util.Locale local=ctx.getLocale();
String title1="测试title";
String sendContext1="测试内容";
msgInfo.setTitle(title1, local);
msgInfo.setBody(sendContext1,local);
IUser iUser;
try {
iUser = UserFactory.getLocalInstance(ctx);
UserInfo userInfo = iUser.getUserInfo(new ObjectUuidPK(ctx.getCaller().toString()));
msgInfo.setId(BOSUuid.create(msgInfo.getBOSType()));//id
msgInfo.setSender(userInfo.getName(local), local);
msgInfo.setPriority(MsgPriority.LOW);
msgInfo.setNreceivers("[预设用户.user]");
msgInfo.setType(MsgType.ONLINE);
msgInfo.setStatus(MsgStatus.UNREADED);
BasReceiverCollection basCol=new BasReceiverCollection();
BasReceiverInfo item=new BasReceiverInfo();
item.setIsShow(true);
item.setDesc("预设用户");
item.setType("3");
item.setValue("256c221a-0106-1000-e000-10d7c0a813f413B7DE7F");//user id
basCol.add(item);
IBMCMessage iBMCMessage = BMCMessageFactory.getLocalInstance(ctx);
iBMCMessage.addHandMsg(msgInfo,basCol);
} catch (BOSException e) {
e.printStackTrace();
} catch (EASBizException e) {
e.printStackTrace();
}