@Override public void beanChangePerformed(BeanEvent<U> evt) { U bean = evt.getBean(); if (!bean.getStatus().isRequest()) return; WeakReference<IConsumerProcess<U>> ref = processes.get(bean.getUniqueId()); try { if (ref == null) { // Might be in submit queue still updateQueue(bean); } else { IConsumerProcess<U> process = ref.get(); if (process != null) { process.getBean().setStatus(bean.getStatus()); process.getBean().setMessage(bean.getMessage()); if (bean.getStatus() == Status.REQUEST_TERMINATE) { processes.remove(bean.getUniqueId()); if (process.isPaused()) process.resume(); process.terminate(); } else if (bean.getStatus() == Status.REQUEST_PAUSE) { process.pause(); } else if (bean.getStatus() == Status.REQUEST_RESUME) { process.resume(); } } } } catch (EventException ne) { logger.error("Internal error, please contact your support representative.", ne); } }
private void executeBean(U bean) throws EventException, InterruptedException { // We record the bean in the status queue if (overrideMap != null && overrideMap.containsKey(bean.getUniqueId())) { U o = overrideMap.remove(bean.getUniqueId()); bean.setStatus(o.getStatus()); } logger.trace("Moving " + bean + " to " + mover.getSubmitQueueName()); mover.submit(bean); // Run the process if (runner == null) { bean.setStatus(Status.FAILED); bean.setMessage("No runner set for consumer " + getName() + ". Nothing run"); status.broadcast(bean); throw new EventException("You must set the runner before executing beans from the queue!"); } if (processes.containsKey(bean.getUniqueId())) { throw new EventException( "The bean with unique id '" + bean.getUniqueId() + "' has already been used. Cannot run the same uuid twice!"); } // We peal off the most recent bean from the submission queue if (bean.getStatus() == Status.REQUEST_TERMINATE) { bean.setStatus(Status.TERMINATED); bean.setMessage("Run aborted before started"); status.broadcast(bean); return; } if (bean.getStatus().isFinal()) return; // This is not the bean you are looking for. IConsumerProcess<U> process = runner.createProcess(bean, status); processes.put(bean.getUniqueId(), new WeakReference<IConsumerProcess<U>>(process)); process.start(); // Depending on the process may run in a separate thread (default is not to) }
protected void updateQueue(U bean) throws EventException { boolean resumeAfter = !awaitPaused; Session session = null; try { pause(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getSubmitQueueName()); QueueBrowser qb = session.createBrowser(queue); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { Message msg = (Message) e.nextElement(); TextMessage t = (TextMessage) msg; String json = t.getText(); final StatusBean b = service.unmarshal(json, getBeanClass()); MessageConsumer consumer = session.createConsumer(queue, "JMSMessageID = '" + msg.getJMSMessageID() + "'"); Message rem = consumer.receive(Constants.getReceiveFrequency()); consumer.close(); if (rem == null && b.getUniqueId().equals(bean.getUniqueId())) { // Something went wrong, not sure why it does this, TODO investigate if (overrideMap == null) overrideMap = new Hashtable<>(7); overrideMap.put(b.getUniqueId(), bean); continue; } MessageProducer producer = session.createProducer(queue); if (b.getUniqueId().equals(bean.getUniqueId())) { b.setStatus(bean.getStatus()); t = session.createTextMessage(service.marshal(b)); t.setJMSMessageID(rem.getJMSMessageID()); t.setJMSExpiration(rem.getJMSExpiration()); t.setJMSTimestamp(rem.getJMSTimestamp()); t.setJMSPriority(rem.getJMSPriority()); t.setJMSCorrelationID(rem.getJMSCorrelationID()); } producer.send(t); producer.close(); } } catch (Exception ne) { throw new EventException("Cannot reorder queue!", ne); } finally { // Only resume if it wasn't in a paused state before this update if (resumeAfter) { resume(); } try { if (session != null) session.close(); } catch (JMSException e) { throw new EventException("Cannot close session!", e); } } }