Jms

Модератор: Absurd

Ответить
michael
Сообщения: 116
Зарегистрирован: 15 июл 2004, 13:06
Откуда: ISRAEL (ранее - из Литвы)
Контактная информация:

На работе поставили MQSERIES (IBM). Зачем? Сам не знаю , но хотят с другой машины подключится и узнать сколько messages есть в queue. Повесили это дело на меня. Про JMS я вообще услышал только когда начал копать как это сделать. MQSERIES API начальство забраковало, значит надо на JMS. Скачал SUN tutorial, но никак не могу понять где там определить подключение к remote machine. Вот код из сановского сампела

http://java.sun.com/j2ee/1.4/docs/tutor ... ducer.java

там испльзуется объект connection, но я так и понял где там URL прописать. Помогите разобратся. Спасибо

/**
* The SimpleProducer class consists only of a main method,
* which sends several messages to a queue or topic.
*
* Run this program in conjunction with SimpleSynchConsumer or
* SimpleAsynchConsumer. Specify a queue or topic name on the
* command line when you run the program. By default, the
* program sends one message. Specify a number after the
* destination name to send that number of messages.
*/
import javax.jms.*;
import javax.naming.*;


public class SimpleProducer {
/**
* Main method.
*
* @param args the destination used by the example
* and, optionally, the number of
* messages to send
*/
public static void main(String[] args) {
final int NUM_MSGS;

if ((args.length < 1) || (args.length > 2)) {
System.out.println("Program takes one or two arguments: " +
"<dest_name> [<number-of-messages>]");
System.exit(1);
}

String destName = new String(args[0]);
System.out.println("Destination name is " + destName);

if (args.length == 2) {
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}

/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
Context jndiContext = null;

try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: " +
e.toString());
System.exit(1);
}

/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
ConnectionFactory connectionFactory = null;
Destination dest = null;

try {
connectionFactory = (ConnectionFactory) jndiContext.lookup(
"jms/JupiterConnectionFactory");
dest = (Destination) jndiContext.lookup(destName);
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
e.printStackTrace();
System.exit(1);
}

/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
Connection connection = null;
MessageProducer producer = null;

try {
connection = connectionFactory.createConnection();

Session session =
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(dest);

TextMessage message = session.createTextMessage();

for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " + message.getText());
producer.send(message);
}

/*
* Send a non-text control message indicating end of
* messages.
*/
producer.send(session.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
}
Ответить