在消息的5个类型中,BytesMessage是移植性最好的。
BytesMessage.writeUTF()
package ch08; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * Test the BytesMessage * @version 2010-10-26 下午02:15:42 * @author DaWei han * @EMAIL [email protected] */ public class QueueSender { public QueueSender() { try { InitialContext context = new InitialContext(); factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue = (Queue) context.lookup("jms/RequestQueue"); con.start(); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } public void senMessage(String message) { try { BytesMessage msg = session.createBytesMessage(); msg.writeUTF("the message is 消息内容!"); javax.jms.QueueSender sender = session.createSender(requestQueue); sender.send(msg); } catch (JMSException e) { e.printStackTrace(); } } public void close() { try { con.close(); } catch (JMSException e) { e.printStackTrace(); } System.exit(0); } /** * @param args */ public static void main(String[] args) { QueueSender sender = new QueueSender(); System.out.println("Please enter the message to send eg:50,2"); Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String line = in.nextLine(); if ("".equals(line) || "exit".equals(line)) { sender.close(); } sender.senMessage(line); } } private Queue requestQueue; private QueueConnection con; private QueueConnectionFactory factory; private QueueSession session; }
BytesMessage.readUTF();
package ch08; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @version 2010-10-26 下午02:16:15 * @author DaWei han * @EMAIL [email protected] */ public class QueueReceiverTest implements MessageListener{ public QueueReceiverTest() { try { InitialContext context = new InitialContext(); QueueConnectionFactory factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue=(Queue) context.lookup("jms/RequestQueue"); con.start(); receiver = session.createReceiver(requestQueue); receiver.setMessageListener(this); System.out.println("QueueReceiverTest waiting the message......."); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { QueueReceiverTest reciver = new QueueReceiverTest(); System.out.println("press the Enter to Exit."); Scanner in = new Scanner(System.in); in.nextLine(); System.exit(1); } public void onMessage(Message arg0) { try { System.out.println("in to the on message method!"); BytesMessage msg = (BytesMessage) arg0; System.out.println("the message is :" + msg.readUTF()); } catch (JMSException e) { e.printStackTrace(); } } private QueueReceiver receiver; private QueueConnection con; private Queue requestQueue; private QueueSession session; }
可以正常工作。
writeBytes
package ch08; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * Test the BytesMessage * @version 2010-10-26 下午02:15:42 * @author DaWei han * @EMAIL [email protected] */ public class QueueSender01 { public QueueSender01() { try { InitialContext context = new InitialContext(); factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue = (Queue) context.lookup("jms/RequestQueue"); con.start(); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } public void senMessage(String message) { try { BytesMessage msg = session.createBytesMessage(); byte[] bys="消息内容".getBytes(); msg.writeBytes(bys); System.out.println(bys.length); javax.jms.QueueSender sender = session.createSender(requestQueue); sender.send(msg); } catch (JMSException e) { e.printStackTrace(); } } public void close() { try { con.close(); } catch (JMSException e) { e.printStackTrace(); } System.exit(0); } /** * @param args */ public static void main(String[] args) { QueueSender01 sender = new QueueSender01(); System.out.println("Please enter the message to send eg:50,2"); Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String line = in.nextLine(); if ("".equals(line) || "exit".equals(line)) { sender.close(); } sender.senMessage(line); } } private Queue requestQueue; private QueueConnection con; private QueueConnectionFactory factory; private QueueSession session; }
BytesMessage.read(byte[])
package ch08; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @version 2010-10-26 下午02:16:15 * @author DaWei han * @EMAIL [email protected] */ public class QueueReceiverTest01 implements MessageListener{ public QueueReceiverTest01() { try { InitialContext context = new InitialContext(); QueueConnectionFactory factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue=(Queue) context.lookup("jms/RequestQueue"); con.start(); receiver = session.createReceiver(requestQueue); receiver.setMessageListener(this); System.out.println("QueueReceiverTest01 waiting the message......."); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { QueueReceiverTest01 reciver = new QueueReceiverTest01(); System.out.println("press the Enter to Exit."); Scanner in = new Scanner(System.in); in.nextLine(); System.exit(1); } public void onMessage(Message arg0) { try { System.out.println("in to the on message method!"); BytesMessage msg = (BytesMessage) arg0; byte[] bys=new byte[(int)msg.getBodyLength()]; System.out.println("1"); msg.readBytes(bys); System.out.println("2"); System.out.println("=====>"+bys.length); String msgstr=new String(bys); System.out.println(msgstr); } catch (JMSException e) { e.printStackTrace(); } } private QueueReceiver receiver; private QueueConnection con; private Queue requestQueue; private QueueSession session; }
BytesMessage.writeUTF
package ch08; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * Test the BytesMessage * @version 2010-10-26 下午02:15:42 * @author DaWei han * @EMAIL [email protected] */ public class QueueSender02 { public QueueSender02() { try { InitialContext context = new InitialContext(); factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue = (Queue) context.lookup("jms/RequestQueue"); con.start(); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } public void senMessage(String message) { try { BytesMessage msg = session.createBytesMessage(); msg.writeUTF("消息内容"); javax.jms.QueueSender sender = session.createSender(requestQueue); sender.send(msg); } catch (JMSException e) { e.printStackTrace(); } } public void close() { try { con.close(); } catch (JMSException e) { e.printStackTrace(); } System.exit(0); } /** * @param args */ public static void main(String[] args) { QueueSender02 sender = new QueueSender02(); System.out.println("Please enter the message to send eg:50,2"); Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String line = in.nextLine(); if ("".equals(line) || "exit".equals(line)) { sender.close(); } sender.senMessage(line); } } private Queue requestQueue; private QueueConnection con; private QueueConnectionFactory factory; private QueueSession session; }
BytesMessage.read(byte[])
package ch08; import java.io.UnsupportedEncodingException; import java.util.Scanner; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @version 2010-10-26 下午02:16:15 * @author DaWei han * @EMAIL [email protected] */ public class QueueReceiverTest02 implements MessageListener{ public QueueReceiverTest02() { try { InitialContext context = new InitialContext(); QueueConnectionFactory factory = (QueueConnectionFactory) context .lookup("ConnectionFactory"); con = factory.createQueueConnection(); session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); requestQueue=(Queue) context.lookup("jms/RequestQueue"); con.start(); receiver = session.createReceiver(requestQueue); receiver.setMessageListener(this); System.out.println("QueueReceiverTest02 waiting the message......."); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { QueueReceiverTest02 reciver = new QueueReceiverTest02(); System.out.println("press the Enter to Exit."); Scanner in = new Scanner(System.in); in.nextLine(); System.exit(1); } public void onMessage(Message arg0) { try { System.out.println("in to the on message method!"); BytesMessage msg = (BytesMessage) arg0; byte[] bys=new byte[(int)msg.getBodyLength()]; System.out.println("1"); msg.readBytes(bys); System.out.println("2"); System.out.println("=====>"+bys.length); String msgstr = null; try { msgstr = new String(bys,"utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(msgstr); } catch (JMSException e) { e.printStackTrace(); } } private QueueReceiver receiver; private QueueConnection con; private Queue requestQueue; private QueueSession session; }
前面的2种 (写入 writeUTF,readUTF),(write(byte[]),readBytes[byte[]]) 都可以正常工作。最后一种交叉(写入 writeUTF,readBytes(byte[]))时 在读取的时候前面总是有个符号去不掉,也复制不了,很逗。