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