コード例 #1
0
ファイル: Producer.java プロジェクト: mariuszs/jms-client
  public static void main(String[] args) {
    try {
      Parameters parameters = new Parameters(args);

      ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(parameters.url);
      Connection connection = connectionFactory.createConnection();
      connection.start();

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Topic destination = session.createTopic(parameters.topic);

      MessageProducer producer = session.createProducer(destination);
      producer.setDeliveryMode(DeliveryMode.PERSISTENT);

      String messageBody = IOUtils.toString(new FileReader(parameters.message));

      TextMessage message = session.createTextMessage(messageBody);
      message.setStringProperty("Channel", parameters.channel);
      message.setJMSExpiration(parameters.expiration);

      LOG.info("Sent message: {}", message);
      producer.send(message);

      session.close();
      connection.close();
    } catch (Exception e) {
      LOG.error("Producing interrupted", e);
    }
  }
コード例 #2
0
  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);
      }
    }
  }