KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > JMSProducerTest


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jfox.jms;
5
6 import javax.jms.BytesMessage JavaDoc;
7 import javax.jms.Connection JavaDoc;
8 import javax.jms.Queue JavaDoc;
9 import javax.jms.QueueSender JavaDoc;
10 import javax.jms.Session JavaDoc;
11 import javax.jms.TextMessage JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13
14 import junit.framework.TestCase;
15
16 /**
17  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
18  */

19
20 public class JMSProducerTest extends TestCase {
21     JMSConnectionFactory jmsConnectionFactory;
22     Connection JavaDoc conn;
23     Session JavaDoc session;
24     Queue JavaDoc queue;
25     QueueSender JavaDoc queueSender;
26
27     public void setUp() throws Exception JavaDoc {
28         super.setUp();
29         InitialContext JavaDoc ctx = new InitialContext JavaDoc();
30         Object JavaDoc cf = ctx.lookup("jms/defaultcf");
31         jmsConnectionFactory = (JMSConnectionFactory) javax.rmi.PortableRemoteObject.narrow(cf, JMSConnectionFactory.class);
32         queue = (Queue JavaDoc)javax.rmi.PortableRemoteObject.narrow(ctx.lookup("jms/defaultQ1"),Queue JavaDoc.class);
33
34         conn = jmsConnectionFactory.createConnection();
35         conn.start();
36         session = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
37         queueSender = (QueueSender JavaDoc)session.createProducer(queue);
38     }
39
40     protected void tearDown() throws Exception JavaDoc {
41         queueSender.close();
42         session.close();
43         conn.close();
44     }
45
46     public void testQueueSend(){
47         try {
48             TextMessage JavaDoc msg = session.createTextMessage("Hello,JMS!");
49             queueSender.send(msg);
50             BytesMessage JavaDoc bmsg = session.createBytesMessage();
51             queueSender.send(bmsg);
52         }
53         catch(Exception JavaDoc e){
54             e.printStackTrace();
55             fail(e.getMessage());
56         }
57     }
58
59     public static void main(String JavaDoc[] args) {
60
61     }
62 }
63
64
Popular Tags