Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.jms.ObjectMessage;
- import javax.jms.Connection;
- import javax.jms.QueueConnection;
- import javax.jms.Session;
- import javax.jms.QueueSession;
- import javax.jms.QueueConnectionFactory;
- import javax.jms.Queue;
- import javax.jms.QueueSender;
- import javax.naming.InitialContext;
- import javax.naming.Context;
- import org.apache.activemq.ActiveMQConnectionFactory;
- public class SimpleProducer {
- private static Properties jndiProps = new Properties();
- static {
- jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
- jndiProps.put(Context.PROVIDER_URL, "rmi://localhost:1099");
- }
- private static InitialContext jndiCtx = null;
- private static QueueConnection connection = null;
- private static QueueSession session = null;
- public SimpleProducer() {
- boolean transacted = false;
- int ackMode = Session.AUTO_ACKNOWLEDGE;
- int numberOfMessages = 50;
- try {
- //QueueConnectionFactory factory = (QueueConnectionFactory)findObjectInJNDI("ConnectionFactory");
- ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
- //Queue queue = (Queue)findObjectInJNDI("dynamicQueues/aqueue");
- connection = factory.createQueueConnection();
- session = connection.createQueueSession(transacted, ackMode);
- Queue queue = session.createQueue("aqueue");
- QueueSender sender = session.createSender(queue);
- connection.start();
- for (int num = 0; num < numberOfMessages; num++) {
- ObjectMessage message = session.createObjectMessage();
- message.setObject(new String(new byte[500]));
- sender.send(message);
- Thread.sleep(10);
- }
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private Object findObjectInJNDI(String name) throws Exception {
- if (null == jndiCtx)
- try {
- jndiCtx = new InitialContext(jndiProps);
- } catch (Exception e) {
- System.out.println("Could not get initial context");
- e.printStackTrace();
- System.exit(-1);
- }
- return jndiCtx.lookup(name);
- }
- public static void main(String[] args) {
- new SimpleProducer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement