daily pastebin goal
59%
SHARE
TWEET

Sender.java

a guest Jun 20th, 2013 6 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.java;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.jms.DeliveryMode;
  6. import javax.jms.JMSException;
  7. import javax.jms.Queue;
  8. import javax.jms.QueueConnection;
  9. import javax.jms.QueueConnectionFactory;
  10. import javax.jms.QueueSender;
  11. import javax.jms.QueueSession;
  12. import javax.jms.Session;
  13. import javax.jms.TextMessage;
  14. import javax.naming.Context;
  15. import javax.naming.InitialContext;
  16. import javax.naming.NamingException;
  17.  
  18. public class sender {
  19.  
  20.         public sender(String search, String input) throws NamingException, JMSException
  21.         {
  22.                 if(search.equals("first")){
  23.                         System.out.println("Finding "+input + "'s first name!");
  24.                 }
  25.                 else{
  26.                         System.out.println("Finding "+input + "'s last name!");
  27.                 }
  28.  
  29.                 Properties env = new Properties();
  30.                 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
  31.                 env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
  32.                 env.put("queue.queue", "nameQueue");
  33.  
  34.                 // get the initial context
  35.                 InitialContext ctx = new InitialContext(env);
  36.  
  37.                 // lookup the queue object
  38.                 Queue queue = (Queue) ctx.lookup("queue");
  39.  
  40.                 // lookup the queue connection factory
  41.                 QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
  42.                 // create a queue connection
  43.                 QueueConnection queueConn = connFactory.createQueueConnection();
  44.  
  45.                 // create a queue session
  46.                 QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);
  47.  
  48.                 // create a queue sender
  49.                 QueueSender queueSender = queueSession.createSender(queue);
  50.                 queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  51.  
  52.                 // find input matching information
  53.                 if(search.equals("first")){
  54.                         TextMessage message = queueSession.createTextMessage(names.getFirst(input));
  55.                         queueSender.send(message);
  56.                         System.out.println("sent: " + message.getText());
  57.                 }
  58.  
  59.                 else if(search.equals("last")){
  60.                         TextMessage message = queueSession.createTextMessage(names.getLast(input));
  61.                         queueSender.send(message);
  62.                         System.out.println("sent: " + message.getText());
  63.                 }
  64.  
  65.                 else{
  66.                         System.out.println("ERROR: ARGUMENT NOT FOUND");
  67.                 }
  68.  
  69.  
  70.                 // close connection
  71.                 queueConn.close();
  72.         }
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top