Skip to content

Instantly share code, notes, and snippets.

@kamcpp
Created November 5, 2014 03:51
Show Gist options
  • Select an option

  • Save kamcpp/7610bface059f69884fa to your computer and use it in GitHub Desktop.

Select an option

Save kamcpp/7610bface059f69884fa to your computer and use it in GitHub Desktop.
JMS Client for JBoss HornetQ
package mytests.jms;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class JMSConsumer {
public static void main(String[] args) throws NamingException, JMSException {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
properties.put(Context.SECURITY_PRINCIPAL, "test2");
properties.put(Context.SECURITY_CREDENTIALS, "test12345");
InitialContext initialContext = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
Destination destination = (Destination) initialContext.lookup("jms/top/abc");
try (JMSContext context = connectionFactory.createContext("test2", "test12345")) {
javax.jms.JMSConsumer jmsConsumer = context.createConsumer(destination);
jmsConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
System.out.println("====> " + message.getBody(String.class));
} catch (JMSException e) {
e.printStackTrace();
}
}
});
while(true);
// initialContext.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment