示例#1
0
  public QLender(String queuecf, String requestQueue) {
    try {
      // Connect to the provider and get the JMS connection
      Context ctx = new InitialContext();
      QueueConnectionFactory qFactory = (QueueConnectionFactory) ctx.lookup(queuecf);
      qConnect = qFactory.createQueueConnection();

      // Create the JMS Session
      qSession = qConnect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // Lookup the request queue
      requestQ = (Queue) ctx.lookup(requestQueue);

      // Now that setup is complete, start the Connection
      qConnect.start();

      // Create the message listener
      QueueReceiver qReceiver = qSession.createReceiver(requestQ);
      qReceiver.setMessageListener(this);

      System.out.println("Waiting for loan requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (NamingException jne) {
      jne.printStackTrace();
      System.exit(1);
    }
  }
  public DigitalLibraryServer() {
    try {
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");

      InitialContext jndi = new InitialContext(properties);
      ConnectionFactory conFactory = (ConnectionFactory) jndi.lookup("XAConnectionFactory");
      connection = conFactory.createConnection();

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

      try {
        counterTopic = (Topic) jndi.lookup("counterTopic");
      } catch (NamingException NE1) {
        System.out.println("NamingException: " + NE1 + " : Continuing anyway...");
      }

      if (null == counterTopic) {
        counterTopic = session.createTopic("counterTopic");
        jndi.bind("counterTopic", counterTopic);
      }

      consumer = session.createConsumer(counterTopic);
      consumer.setMessageListener(this);
      System.out.println("Server started waiting for client requests");
      connection.start();
    } catch (NamingException NE) {
      System.out.println("Naming Exception: " + NE);
    } catch (JMSException JMSE) {
      System.out.println("JMS Exception: " + JMSE);
      JMSE.printStackTrace();
    }
  }
  public void onMessage(Message inMessage) {
    TextMessage msg = null;

    try {
      if (inMessage instanceof TextMessage) {
        msg = (TextMessage) inMessage;
        System.out.println("MESSAGE BEAN: Message received: " + msg.getText());
        long sleepTime = msg.getLongProperty("sleeptime");
        System.out.println("Sleeping for : " + sleepTime + " milli seconds ");
        Thread.sleep(sleepTime);
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        TextMessage message = queueSession.createTextMessage();

        message.setText("REPLIED:" + msg.getText());
        message.setIntProperty("replyid", msg.getIntProperty("id"));
        System.out.println("Sending message: " + message.getText());
        queueSender.send(message);
      } else {
        System.out.println("Message of wrong type: " + inMessage.getClass().getName());
      }
    } catch (JMSException e) {
      e.printStackTrace();
    } catch (Throwable te) {
      te.printStackTrace();
    } finally {
      try {
        queueSession.close();
        queueConnection.close();
      } catch (Exception e) {
      }
    }
  } // onMessage
示例#4
0
 private void exit() {
   try {
     qConnect.close();
   } catch (JMSException jmse) {
     jmse.printStackTrace();
   }
   System.exit(0);
 }
示例#5
0
  public void onMessage(Message message) {

    try {
      boolean accepted = false;

      // Get the data from the message
      MapMessage msg = (MapMessage) message;
      double salary = msg.getDouble("Salary");
      double loanAmt = msg.getDouble("LoanAmount");

      // Determine whether to accept or decline the loan
      if (loanAmt < 200000) {
        accepted = (salary / loanAmt) > .25;
      } else {
        accepted = (salary / loanAmt) > .33;
      }
      System.out.println(
          ""
              + "Percent = "
              + (salary / loanAmt)
              + ", loan is "
              + (accepted ? "Accepted!" : "Declined"));

      // Send the results back to the borrower
      TextMessage tmsg = qSession.createTextMessage();
      tmsg.setText(accepted ? "Accepted!" : "Declined");

      // correlation
      tmsg.setJMSCorrelationID(message.getJMSMessageID());

      // Create the sender and send the message
      qSender = qSession.createSender((Queue) message.getJMSReplyTo());
      qSender.send(tmsg);

      System.out.println("\nWaiting for loan requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (Exception jmse) {
      jmse.printStackTrace();
      System.exit(1);
    }
  }
示例#6
0
文件: Recruiter.java 项目: saquil/JMS
  public void onMessage(Message message) {

    try {
      boolean accepted = false;

      // Get the data from the message
      MapMessage msg = (MapMessage) message;
      double salary = msg.getDouble("Salary");
      double expAmt = msg.getDouble("Years’ experience");

      // Determine whether to accept or decline the loan
      if (expAmt < 200000) {
        accepted = (salary / expAmt) > .25;
      } else {
        accepted = (salary / expAmt) > .33;
      }
      if (salary <= 32000) {
        accepted = true;
      } else {
        if (expAmt == 0) accepted = false;
        else accepted = ((double) (expAmt - 32000) / expAmt) < 3000.;
      }
      System.out.println(" Salary proposal is " + (accepted ? "Accepted!" : "Declined"));

      // Send the results back to the borrower
      TextMessage tmsg = qSession.createTextMessage();
      tmsg.setText(accepted ? "Accepted!" : "Declined");
      tmsg.setJMSCorrelationID(message.getJMSMessageID());

      // Create the sender and send the message
      QueueSender qSender = qSession.createSender((Queue) message.getJMSReplyTo());
      qSender.send(tmsg);

      System.out.println("\nWaiting for salary requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (Exception jmse) {
      jmse.printStackTrace();
      System.exit(1);
    }
  }
示例#7
0
  public tibjmsTopicSubscriber(String[] args) {

    parseArgs(args);

    /* print parameters */
    System.err.println(
        "\n------------------------------------------------------------------------");
    System.err.println("tibjmsTopicSubscriber SAMPLE");
    System.err.println("------------------------------------------------------------------------");
    System.err.println(
        "Server....................... " + ((serverUrl != null) ? serverUrl : "localhost"));
    System.err.println(
        "User......................... " + ((userName != null) ? userName : "******"));
    System.err.println("Topic........................ " + topicName);
    System.err.println(
        "------------------------------------------------------------------------\n");

    try {
      tibjmsUtilities.initSSLParams(serverUrl, args);
    } catch (JMSSecurityException e) {
      System.err.println(
          "JMSSecurityException: " + e.getMessage() + ", provider=" + e.getErrorCode());
      e.printStackTrace();
      System.exit(0);
    }

    if (topicName == null) {
      System.err.println("Error: must specify topic name");
      usage();
    }

    System.err.println("Subscribing to topic: " + topicName);

    try {
      TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverUrl);

      TopicConnection connection = factory.createTopicConnection(userName, password);

      TopicSession session =
          connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

      /*
       * Use createTopic() to enable subscriptions to dynamic topics.
       */
      javax.jms.Topic topic = session.createTopic(topicName);

      TopicSubscriber subscriber = session.createSubscriber(topic);

      connection.start();

      /* read topic messages */
      while (true) {
        javax.jms.Message message = subscriber.receive();
        if (message == null) break;

        System.err.println("Received message: " + message);
      }

      connection.close();
    } catch (JMSException e) {
      System.err.println("JMSException: " + e.getMessage() + ", provider=" + e.getErrorCode());
      e.printStackTrace();
      System.exit(0);
    }
  }