📄 simplequeuesender.java
字号:
package demojms;
/*
* @(#)SimpleQueueSender.java
*/
import javax.jms.*;
import javax.naming.*;
import java.util.*;
public class SimpleQueueSender {
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;
/*if ( (args.length < 1) || (args.length > 2) ) {
System.out.println("Usage: java SimpleQueueSender " +
"<queue-name> [<number-of-messages>]");
System.exit(1);
}*/
queueName = "TheQue";
System.out.println("Queue name is " + queueName);
/*if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}*/
/*
* 如果不存在JNDI InitialContext对象,就创建一个
*/
try {
Properties properties = null;
String url = "t3://127.0.0.1:7001";
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
jndiContext = new InitialContext(properties);
//System.out.println(jndiContext);
}
catch (NamingException e) {
System.out.println("Could not create JNDI " +
"context: " + e.toString());
System.exit(1);
}
/*
* 寻找connection factory和queue,如果两者读不存在,则退出
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory1");
queue = (Queue) jndiContext.lookup("TheQue");
}
catch (NamingException e) {
System.out.println("JNDI lookup failed: " +
e.toString());
System.exit(1);
}
/*
* 创建一个connection,在从connection创建session
* 创建sender和文本消息,发送消息
* 最后,关闭connection
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < 5; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}
/*
* 发送非文本的消息,描述消息结束
*/
queueSender.send(queueSession.createMessage());
}
catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
}
finally {
if (queueConnection != null) {
try {
queueConnection.close();
}
catch (JMSException e) {}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -