Example #1
0
 /**
  * Returns a new JMS Message Consumer.
  *
  * @param session A JMS Session
  * @return A JMS Message Consumer
  * @throws JMSException
  */
 public javax.jms.MessageConsumer newConsumer(Session session) throws JMSException {
   if (session == null) {
     logger.error(nameString() + " cannot create JMS Consumer. Invalid session.");
     return null;
   }
   if (!createDestIfAbsent(session)) {
     logger.error(
         nameString() + " cannot create JMS Consumer. " + "Destination queue is invalid.");
     return null;
   }
   javax.jms.MessageConsumer consumer;
   if (isVersion11) {
     consumer = session.createConsumer(queue);
   } else {
     consumer = ((QueueSession) session).createReceiver((Queue) queue);
   }
   if (logger.isDebugEnabled()) {
     logger.debug(
         nameString()
             + " created JMS Message Consumer to destination ["
             + queue.toString()
             + "].");
   }
   return consumer;
 }
Example #2
0
  public void sendObject(
      Destination destination, final Serializable object, final String type, final String value) {
    try {
      jmsTemplate.send(
          destination,
          new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
              ObjectMessage objectMessage = session.createObjectMessage(object);
              objectMessage.setStringProperty(type, value);
              return objectMessage;
            }
          });

    } catch (Exception e) {
      logger.debug("消息发送失败" + destination.toString() + ":" + object);
      e.printStackTrace();
      FailedJms failedJms = new FailedJms();
      failedJms.setId(ObjectId.get());
      failedJms.setDestination(destination);
      failedJms.setMessage(object);
      failedJms.setFailedTime(new Date());
      Map<String, String> properties = new HashMap<String, String>();
      properties.put(type, value);
      failedJms.setProperties(properties);
      saveFailedJms(failedJms);
    }
  }
  // ~ ==================================================
  public void sendMessage(Destination destination, String text) {
    String destinationName = destination.toString();

    if (destination instanceof Topic) {
      sendTopic(destinationName, text);
    } else {
      sendQueue(destinationName, text);
    }
  }
  public static void main(String args[]) {
    Connection connection = null;

    try {
      // JNDI lookup of JMS Connection Factory and JMS Destination
      Context context = new InitialContext();
      ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);
      Destination destination = (Destination) context.lookup(DESTINATION_NAME);

      connection = factory.createConnection();
      connection.start();

      Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer consumer = session.createConsumer(destination);

      LOG.info(
          "Start consuming messages from "
              + destination.toString()
              + " with "
              + MESSAGE_TIMEOUT_MILLISECONDS
              + "ms timeout");

      // Synchronous message consumer
      int i = 1;
      while (true) {
        Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS);
        if (message != null) {
          if (message instanceof TextMessage) {
            String text = ((TextMessage) message).getText();
            LOG.info("Got " + (i++) + ". message: " + text);
          }
        } else {
          break;
        }
      }

      consumer.close();
      session.close();
    } catch (Throwable t) {
      LOG.error("Error receiving message", t);
    } finally {
      // Cleanup code
      // In general, you should always close producers, consumers,
      // sessions, and connections in reverse order of creation.
      // For this simple example, a JMS connection.close will
      // clean up all other resources.
      if (connection != null) {
        try {
          connection.close();
        } catch (JMSException e) {
          LOG.error("Error closing connection", e);
        }
      }
    }
  }
  public void removeMessageConsumer(ProxyMessageConsumer messageConsumer) {
    Destination destination = messageConsumer.getDestination();
    String destinationName = destination.toString();
    List<ProxyMessageConsumer> messageConsumers = messageConsumerMap.get(destinationName);

    if (messageConsumers == null) {
      return;
    }

    messageConsumers.remove(messageConsumer);
  }
  public MessageConsumer createConsumer(Destination destination, ProxySession session) {
    String destinationName = destination.toString();
    List<ProxyMessageConsumer> messageConsumers = messageConsumerMap.get(destinationName);

    if (messageConsumers == null) {
      messageConsumers = new ArrayList<ProxyMessageConsumer>();
      messageConsumerMap.put(destinationName, messageConsumers);
    }

    ProxyMessageConsumer messageConsumer = new ProxyMessageConsumer(session);
    messageConsumer.setDestination(destination);
    messageConsumers.add(messageConsumer);

    return messageConsumer;
  }
 public JMSProducerStatsImpl(JMSSessionStatsImpl sessionStats, Destination destination) {
   super(sessionStats);
   if (destination != null) {
     this.destination = destination.toString();
   }
 }
Example #8
0
 public String toString() {
   return (dest_ != null) ? dest_.toString() : "null";
 }