private void close(QueueSender sender) {
   try {
     if (sender != null) {
       sender.close();
     }
   } catch (Exception e) {
   }
 }
 public void sendException(QueueSession session, Queue queue, Throwable exception)
     throws JMSException {
   QueueSender sender = session.createSender(queue);
   ObjectMessage message = session.createObjectMessage(exception);
   try {
     sender.send(message);
   } finally {
     sender.close();
   }
 }
Esempio n. 3
0
  protected synchronized void closeConnection() {
    try {
      if (null != m_sender) m_sender.close();
      if (null != m_session) m_session.close();
      if (null != m_connection) m_connection.close();
    } catch (Exception e) {
      getErrorHandler().error("Error closing connection", e, null);
    }

    m_sender = null;
    m_session = null;
    m_connection = null;
  }
  /**
   * Envia un mensaje a JMS.
   *
   * @param mensaje
   */
  public void send(String mensaje) throws Exception {
    try {

      QueueSender queueSender = queueSession.createSender(cola);
      TextMessage textMessage = queueSession.createTextMessage(mensaje);
      queueSender.send(textMessage);
      queueSender.close();

    } catch (JMSException e) {
      e.printStackTrace();
    } finally {
      c.close();
    }
  }
Esempio n. 5
0
  public static void queueSend(
      QueueConnection cnn,
      String queueName,
      String payload,
      boolean transacted,
      int ack,
      String replyTo)
      throws JMSException {
    QueueSession session = cnn.createQueueSession(transacted, ack);
    Queue queue = session.createQueue(queueName);
    QueueSender sender = session.createSender(queue);
    TextMessage msg = session.createTextMessage();
    msg.setText(payload);
    msg.setJMSDeliveryMode(ack);
    if (replyTo != null) {
      msg.setJMSReplyTo(session.createQueue(replyTo));
    }

    sender.send(msg);
    sender.close();
    session.close();
  }
  public static void main(String[] args) throws Exception {
    InputStream is = TibcoSender.class.getClassLoader().getResourceAsStream("messages.properties");
    if (is == null) {
      System.out.println("message.properties must be in classpath");
      return;
    }
    Properties messages = new Properties();
    messages.load(is);
    is.close();
    QueueConnection queueCon = null;
    QueueSession queueSession = null;
    QueueSender sender = null;
    long idStart = 100003000l;
    Scanner in = new Scanner(System.in);
    System.out.print(
        "Enter amount of message you wish to send (set 0 to read amount from init.properties): ");
    int count = 0;
    try {
      count = in.nextInt();
    } catch (Exception e) {
      System.out.println("Error in reading input.");
    }
    Properties initial = new Properties();
    is = TibcoSender.class.getClassLoader().getResourceAsStream("init.properties");
    if (is != null) {
      initial.load(is);
      String value = initial.getProperty("start.id");
      if (value != null) idStart = Long.valueOf(value).longValue();
      value = initial.getProperty("count");
      if (value != null && count <= 0) count = Integer.valueOf(value).intValue();
    }

    if (args.length > 0) {
      try {
        idStart = Long.valueOf(args[0]);
        if (args.length > 1) count = Integer.valueOf(args[1]);
      } catch (Exception e) {
      }
    }
    initial.setProperty("count", "" + count);
    initial.setProperty("start.id", "" + (idStart + count));
    OutputStream os = null;
    if (is != null) {
      os =
          new FileOutputStream(
              new File(
                  TibcoSender.class.getClassLoader().getResource("init.properties").getPath()));
      is.close();
    } else {
      os = new FileOutputStream(new File("init.properties"));
    }
    initial.store(os, "");
    os.close();
    Properties config = new Properties();
    is = TibcoSender.class.getClassLoader().getResourceAsStream("config.properties");
    if (is != null) {
      config.load(is);
      is.close();
    }
    try {
      // messages

      // InitialContext properties
      Properties props = new Properties();
      props.setProperty(Context.INITIAL_CONTEXT_FACTORY, config.getProperty("JNDI_ICF"));
      props.setProperty(Context.PROVIDER_URL, config.getProperty("JNDI_URL"));

      // Jndi initialization
      InitialContext jndiContext = new InitialContext(props);

      // get the connection factory
      QueueConnectionFactory qcf =
          (QueueConnectionFactory) jndiContext.lookup(config.getProperty("JMS_QCF"));
      // and open a connection
      String user = config.getProperty("JMS_USER");
      String password = config.getProperty("JMS_PASSWORD");
      if (user != null && !user.isEmpty() && password != null && !password.isEmpty())
        queueCon = qcf.createQueueConnection(user, password);
      else queueCon = qcf.createQueueConnection();

      // Create a queue session using the connection
      queueSession = queueCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // get handle on queue, create a sender and send the message
      Queue topic = (Queue) jndiContext.lookup(config.getProperty("JMS_QUEUE"));
      sender = queueSession.createSender(topic);

      long i = 0; // new message index to be inserted into content of the message
      String[] cps = {
        "USD/HUF", "GBP/USD", "GBP/USD", "GBP/USD", "GBP/USD", "AUD/USD", "AUD/USD", "AUD/USD",
        "AUD/USD", "EUR/NOK", "USD/CZK", "EUR/USD", "EUR/JPY", "USD/HUF", "USD/SGD", "SGD/CHF"
      };
      String[] valuedates =
          new String
              [0]; // {"20110803", "20110810", "20110817", "20110824", "20110831", "20110913",
                   // "20110926"};
      String[] tenors = {"SPOT", "TN", "1W", "1W", "SPOT", "2W", "1M", "1W", "SPOT", "SPOT", "1W"};
      String[] clients = {
        "002-230", "003-0582", "004-0986", "Paypal1US", "003-0291"
      }; // FXB_OPERATINGCLIENT.APICLIENTID
      String tradeDate = date.format(new Date());

      for (int j = 0; j < count; j++) {
        i = idStart + j;
        String message =
            createMessage(
                idStart, i, j, cps, valuedates, tenors, clients, tradeDate, config, messages);

        // send message to topic at server.
        writeMessage(message, queueSession, sender, i);
      }

    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    } finally {
      // close sender, topic session and topic connection
      if (sender != null) {
        sender.close();
        sender = null;
      }
      if (queueSession != null) {
        queueSession.close();
        queueSession = null;
      }
      if (queueCon != null) {
        queueCon.close();
        queueCon = null;
      }
    }
  }
 @Test(timeout = 30000)
 public void testSenderCloseAgain() throws Exception {
   // Close it again (closing the session should have closed it already).
   sender.close();
 }