Advertisement
Guest User

Sender

a guest
May 3rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. /**
  2.  * Created by Tim Oomis on 1-5-2017.
  3.  */
  4.  
  5. import javax.jms.*;
  6. import javax.naming.Context;
  7. import javax.naming.InitialContext;
  8. import javax.naming.NamingException;
  9. import java.util.Properties;
  10. import java.util.logging.Logger;
  11.  
  12. public class MessageSender
  13. {
  14.     private Context namingContext = null;
  15.     private ConnectionFactory connectionFactory = null;
  16.     private Destination destination;
  17.  
  18.     private static final Logger log = Logger.getLogger(MessageSender.class.getName());
  19.  
  20.     // Set up all the default values
  21.     private static final String DEFAULT_MESSAGE = "HOLY SHIT WERKT DIT?!";
  22.     private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
  23.     private static final String DEFAULT_DESTINATION = "jms/queue/kwettergo";
  24.     private static final String DEFAULT_MESSAGE_COUNT = "5";
  25.     private static final String DEFAULT_USERNAME = "kwetterjms";
  26.     private static final String DEFAULT_PASSWORD = "kwetterjms1!";
  27.     private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
  28.     private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";
  29.  
  30.     public MessageSender() {
  31.  
  32.  
  33.         try {
  34.             String userName = System.getProperty("username", DEFAULT_USERNAME);
  35.             String password = System.getProperty("password", DEFAULT_PASSWORD);
  36.  
  37.             // Set up the namingContext for the JNDI lookup
  38.             final Properties env = new Properties();
  39.             env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
  40.             env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
  41.             env.put(Context.SECURITY_PRINCIPAL, userName);
  42.             env.put(Context.SECURITY_CREDENTIALS, password);
  43.             this.namingContext = new InitialContext(env);
  44.  
  45.             // Perform the JNDI lookups
  46.             String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
  47.  
  48.             log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
  49.             this.connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
  50.             log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
  51.  
  52.             String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
  53.             log.info("Attempting to acquire destination \"" + destinationString + "\"");
  54.             this.destination = (Destination) namingContext.lookup(destinationString);
  55.             log.info("Found destination \"" + destinationString + "\" in JNDI");
  56.         }
  57.         catch (NamingException exception) {
  58.             exception.printStackTrace();
  59.         }
  60.         finally {
  61.             if (namingContext != null) {
  62.                 try {
  63.                     namingContext.close();
  64.                 } catch (NamingException e) {
  65.                     log.severe(e.getMessage());
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.     public boolean send(String text, String mail)
  72.     {
  73.         JMSContext context = null;
  74.         try
  75.         {
  76.             context = this.connectionFactory.createContext(DEFAULT_USERNAME, DEFAULT_PASSWORD);
  77.             TextMessage textMessage = context.createTextMessage();
  78.             textMessage.setText("KWETTER_GO");
  79.             textMessage.setStringProperty("text", text);
  80.             textMessage.setStringProperty("mail", mail);
  81.             context.createProducer().send(this.destination, textMessage);
  82.             return true;
  83.         }
  84.         catch (Exception e)
  85.         {
  86.             log.severe(e.getMessage());
  87.         }
  88.         finally {
  89.             context.close();
  90.         }
  91.         return false;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement