Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Tim Oomis on 1-5-2017.
- */
- import javax.jms.*;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import java.util.Properties;
- import java.util.logging.Logger;
- public class MessageSender
- {
- private Context namingContext = null;
- private ConnectionFactory connectionFactory = null;
- private Destination destination;
- private static final Logger log = Logger.getLogger(MessageSender.class.getName());
- // Set up all the default values
- private static final String DEFAULT_MESSAGE = "HOLY SHIT WERKT DIT?!";
- private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
- private static final String DEFAULT_DESTINATION = "jms/queue/kwettergo";
- private static final String DEFAULT_MESSAGE_COUNT = "5";
- private static final String DEFAULT_USERNAME = "kwetterjms";
- private static final String DEFAULT_PASSWORD = "kwetterjms1!";
- private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
- private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";
- public MessageSender() {
- try {
- String userName = System.getProperty("username", DEFAULT_USERNAME);
- String password = System.getProperty("password", DEFAULT_PASSWORD);
- // Set up the namingContext for the JNDI lookup
- final Properties env = new Properties();
- env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
- env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
- env.put(Context.SECURITY_PRINCIPAL, userName);
- env.put(Context.SECURITY_CREDENTIALS, password);
- this.namingContext = new InitialContext(env);
- // Perform the JNDI lookups
- String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
- log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
- this.connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
- log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
- String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
- log.info("Attempting to acquire destination \"" + destinationString + "\"");
- this.destination = (Destination) namingContext.lookup(destinationString);
- log.info("Found destination \"" + destinationString + "\" in JNDI");
- }
- catch (NamingException exception) {
- exception.printStackTrace();
- }
- finally {
- if (namingContext != null) {
- try {
- namingContext.close();
- } catch (NamingException e) {
- log.severe(e.getMessage());
- }
- }
- }
- }
- public boolean send(String text, String mail)
- {
- JMSContext context = null;
- try
- {
- context = this.connectionFactory.createContext(DEFAULT_USERNAME, DEFAULT_PASSWORD);
- TextMessage textMessage = context.createTextMessage();
- textMessage.setText("KWETTER_GO");
- textMessage.setStringProperty("text", text);
- textMessage.setStringProperty("mail", mail);
- context.createProducer().send(this.destination, textMessage);
- return true;
- }
- catch (Exception e)
- {
- log.severe(e.getMessage());
- }
- finally {
- context.close();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement