JMSTestClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package userjmssesenbdb;
- import java.util.*;
- import javax.jms.*;
- import javax.naming.*;
- public class JMSTestClient {
- public JMSTestClient() {
- }
- public final static String
- JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
- public final static String URL = "t3://127.0.0.1:7001";
- private static final String QUEUE_CONNECTION_FACTORY =
- "DemoJMSConnectionFactory";
- private static final String QUEUE = "DemoJMSQueue";
- private static InitialContext getInitialContext(String url) throws
- NamingException {
- Hashtable env = new Hashtable();
- env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
- env.put(Context.PROVIDER_URL, url);
- return new InitialContext(env);
- }
- public static void main(String[] args) throws Exception {
- Context ctx = getInitialContext(URL);
- QueueConnectionFactory qConnFact =
- (QueueConnectionFactory) ctx.lookup(QUEUE_CONNECTION_FACTORY);
- QueueConnection qConn = qConnFact.createQueueConnection();
- QueueSession qSess = qConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
- Queue q = (Queue) ctx.lookup(QUEUE);
- System.out.println(q);
- QueueSender qSend = qSess.createSender(q);
- TextMessage txtMsg = qSess.createTextMessage("c");
- System.out.println("Sending a message to queue");
- qSend.send(txtMsg);
- //---------------------------------------------------------//
- txtMsg = qSess.createTextMessage("a");
- System.out.println("Sending a message to queue");
- qSend.send(txtMsg);
- //---------------------------------------------------------//
- qConn.close();
- System.exit(0);
- }
- }