jbpm human task 源代码分析 - I中为执行jbpm human task服务器端的代码,本文集中与执行human task时客户端如何连接到服务器,进行与服务器交互,jBPM 5执行human task是与服务器通信可以使用三种方式:
jbpm human task 源代码分析 - I中是使用jms,本文客户端代码分析也使用jms,所设计到的类注意包括JMSTaskClientConnector,JMSTaskClientHandler等。
org.jbpm.task.service.TaskClientConnector接口定义了客户端连接到human task服务器端的方法,接口中定义了如下方法:
public boolean connect(); public boolean connect(String address, int port); public void disconnect() throws Exception; public void write(Object message); public BaseHandler getHandler(); public String getName(); public AtomicInteger getCounter();
connect()初始化JMS发送消息相关的Session,Producer等,顺序大致如下:
通过JNDI查询获取QueueConnectionFactory,通过QueueConnectionFactory创建QueueConnection,通过QueueConnection创建QueueSession,通过QueueSession创建MessageProducer,如果以上过程成功返回true。相关代码段如下:
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(connFactoryName); this.connection = factory.createQueueConnection(); this.producerSession = this.connection.createQueueSession(transactedQueue, acknowledgeMode); this.consumerSession = this.connection.createQueueSession(transactedQueue, acknowledgeMode); this.taskServerQueue = this.producerSession.createQueue(taskServerQueueName); this.responseQueue = this.consumerSession.createQueue(responseQueueName); this.producer = this.producerSession.createProducer(this.taskServerQueue); this.connection.start(); return true;
public JMSTaskClientConnector(String name, BaseClientHandler handler, Properties connectionProperties, Context context) { if (name == null) { throw new IllegalArgumentException("Name can not be null"); } this.name = name; this.handler = handler; this.connectionProperties = connectionProperties; this.context = context; this.counter = new AtomicInteger(); if(System.getProperty("enableLog") != null) this.enableLog = Boolean.parseBoolean(System.getProperty("enableLog")); }
JMSTaskClientConnector构造方法中需要传入客户端连接处理器JMSTaskClientHandler,这里我们分析该类,首先给出类图,如下:
org.jbpm.task.service.BaseHandler定义了一个方法:
public void addResponseHandler(int id, ResponseHandler responseHandler);
protected Map<Integer, ResponseHandler> responseHandlers; public BaseClientHandler() { responseHandlers = new HashMap<Integer, ResponseHandler>(); } public void addResponseHandler(int id, ResponseHandler responseHandler) { responseHandlers.put( id, responseHandler ); }
public void messageReceived(Session session, Object message, Destination destination, String selector) throws Exception { String name = ""; if (destination instanceof Queue) { name = ((Queue) destination).getQueueName(); } else if (destination instanceof Topic) { name = ((Topic) destination).getTopicName(); } MessageProducer producer = (MessageProducer) this.producers.get(name); if (producer == null) { producer = session.createProducer(destination); this.producers.put(name, producer); } this.handler.messageReceived(new JMSSessionWriter(session, producer, selector), message); }