1. WebSocketServiceImpl 实现
@Service
public class WebSocketServiceImpl implements IWebSocketService {

    private static final int MAXSIZE = 10;

    @Resource
    private TaskService taskService;

    //存放webSocketSession
    private static ConcurrentMap<String, WebSocketSession> websockets = new ConcurrentHashMap();

    @Override
    public void addSession(String userId, WebSocketSession session) {
        websockets.put(userId, session);
    }

    @Override
    public void removeSession(String userId) {
        websockets.remove(userId);
    }

    @Override
    public void sendMessage(String userId, TaskEntity taskEntity, boolean flag, int second) {
        new Thread(() -> {
            try {
                if (second > 0) {
                    Thread.sleep(second * 1000);
                }
                List<Task> taskList = taskService.createTaskQuery().taskCandidateOrAssigned(userId).list();
                taskList = taskList.stream().limit(MAXSIZE).collect(Collectors.toList());
                SocketTaskVo stv = null;
                if (CollectionUtils.isNotEmpty(taskList)) {
                    stv = new SocketTaskVo((long) taskList.size());
                    List<TaskVo> datas = new ArrayList<>();
                    taskList.forEach(task -> datas.add(new TaskVo(task.getName(), task.getId())));
                    stv.setDatas(datas);
                } else {
                    stv = new SocketTaskVo(0L);
                    List<TaskVo> datas = new ArrayList<>();
                    stv.setDatas(datas);
                }
                String message = JSON.toJSONString(stv, true);
                WebSocketSession session = websockets.get(userId);
                this.sendMessage(session, message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void sendMessage(String userId) {
        List<Task> taskList = taskService.createTaskQuery().taskCandidateOrAssigned(userId).list();
        taskList = taskList.stream().limit(MAXSIZE).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(taskList)) {
            SocketTaskVo stv = new SocketTaskVo((long) taskList.size());
            List<TaskVo> datas = new ArrayList<>();
            taskList.forEach(task -> datas.add(new TaskVo(task.getName(), task.getId())));
            stv.setDatas(datas);
            String message = JSON.toJSONString(stv, true);
            WebSocketSession session = websockets.get(userId);
            this.sendMessage(session, message);
        }
    }

    @Override
    public void sendMessage(String userId, String message) {
        WebSocketSession session = websockets.get(userId);
        this.sendMessage(session, message);
    }

    @Override
    public void sendMessage(WebSocketSession session, String message) {
        if (session != null) {
            TextMessage textMessage = new TextMessage(message);
            try {
                session.sendMessage(textMessage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendAllMessage(String message) {
        websockets.forEach((k, v) -> sendMessage(v, message));
    }
}

如何监控任务呢?

  1. 配置全局任务监听 processEngineConfiguration 配置
<!-- 全局事件 -->
<property name="typedEventListeners">
    <map>
        <!-- 当任务创建之时触发 -->
        <entry key="TASK_CREATED">
            <list>
                <ref bean="globalTaskListener"/>
            </list>
        </entry>
        <!-- 当任务完成之时触发 -->
        <entry key="TASK_COMPLETED">
            <list>
                <ref bean="globalTaskListener"/>
            </list>
        </entry>
    </map>
</property>
  1. globalTaskListener具体实现
@Service
public class GlobalTaskListener implements ActivitiEventListener {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void onEvent(ActivitiEvent event) {
        //触发是发送自己的消息
        IWebSocketService webSocketService = SpringContext.getBean("webSocketServiceImpl");
        if (event.getType().equals(ActivitiEventType.TASK_CREATED)) {
            ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
            TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
            String assignee = taskEntity.getAssignee();
            webSocketService.sendMessage(assignee, taskEntity, true,3);
        } else if (event.getType().equals(ActivitiEventType.TASK_COMPLETED)) {
            ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
            TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
            String assignee = taskEntity.getAssignee();
            webSocketService.sendMessage(assignee, taskEntity, false,3);
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}