`

BytesMessage

    博客分类:
  • jms
阅读更多

在消息的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  handawei_1@126.com
 */
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  handawei_1@126.com
 */
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  handawei_1@126.com
 */
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  handawei_1@126.com
 */
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  handawei_1@126.com
 */
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  handawei_1@126.com
 */
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[]))时 在读取的时候前面总是有个符号去不掉,也复制不了,很逗。

 

  • 大小: 15.7 KB
分享到:
评论

相关推荐

    javax.jms.jar

    javax.jms.BytesMessage.class javax.jms.Message.class javax.jms.JMSException.class javax.jms.Destination.class javax.jms.DeliveryMode.class javax.jms.Connection.class javax.jms.Session.class javax.jms....

    javax.jms-1.1.jar

    4. **消息类型**:JMS定义了四种消息类型:`TextMessage`(文本消息)、`ObjectMessage`(序列化Java对象)、`BytesMessage`(字节流)和`MapMessage`(键值对形式的数据)。 5. **事务管理**:JMS支持两种事务模式...

    ActiveMQ常见消息类型.docx

    发送BytesMessage时,可以提供字节数组,接收端通过`BytesMessage`的`readBytes()`方法读取。 5. **StreamMessage**: 类似于BytesMessage,但允许连续读写字节流,适合大数据量的传输。发送和接收StreamMessage...

    JMS、XML 和异构企业.doc

    TextMessage和BytesMessage适合携带XML数据,因为它们是非结构化的,能容纳任意数据,而XML的解析和验证可以通过现有的Java库(如XMLValidator)轻松完成。 【代码示例】发送XML消息的代码示例展示了如何使用JNDI...

    SOAP over JMS Protocal

    当前,WebSphere的客户端实现仅使用`BytesMessage`,这是考虑到未来可能需要传输附件的需求,但此策略在未来版本中可能会有所改变。 2. **设置JMS消息属性**:在将消息发送到目标队列或主题之前,客户端必须为JMS...

    javax.jms.jar下载

    javax.jms.BytesMessage.class javax.jms.Connection.class javax.jms.ConnectionConsumer.class javax.jms.ConnectionFactory.class javax.jms.ConnectionMetaData.class javax.jms.DeliveryMode.class javax.jms....

    JMS入门

    JMS定义了五种消息类型,包括两种非结构化消息(TextMessage和BytesMessage)和三种结构化消息(MapMessage、ObjectMessage和StreamMessage)。非结构化消息在实际应用中更常见,因为它们的灵活性高,可以与XML结合...

    JMS开发例子.pdf

    在本例中,我们主要讨论非结构化消息,即TextMessage和BytesMessage,它们允许灵活的数据交换,尤其是与XML结合时,可以实现更便捷的扩展。对于Java-XML之间的转换,可以参考JAXB(Java Architecture for XML ...

    点对点消息模型示例

    2. **消息格式**:JMS支持多种消息类型,包括TextMessage、ObjectMessage、BytesMessage等,每种类型适用于不同种类的数据传输。例如,TextMessage用于传输文本数据,ObjectMessage可以封装Java对象,BytesMessage则...

    ACTIVEMQ C#下的例子

    这涉及到文件的读取和流操作,如使用`FileStream`读取文件,然后将`FileStream`转换为`MemoryStream`,最后将`MemoryStream`的内容写入BytesMessage。 在提供的"WindowsFormsApplication2"可能是一个示例项目,包含...

    activemq-cpp开发手册.doc

    根据数据类型,消息可以分为TextMessage、ObjectMessage、MapMessage、BytesMessage、StreamMessage和无有效负载的Message。异步消息处理特性使得发送方无需等待响应,可以立即执行其他操作,提高了系统性能。 在...

    JMS教程 java message service

    2. 字节消息(BytesMessage):存储字节流数据,可以是任意类型的数据。 3. 对象消息(ObjectMessage):封装Java对象,允许在消息中传输Java对象。 4. 映射消息(MapMessage):提供键值对结构,方便存储和检索数据...

    activemq学习资料.docx

    * 消息体(Body):JMS定义的消息类型有TextMessage、MapMessage、BytesMessage、StreamMessage和ObjectMessage。 JMS可靠性机制 * 确认(Acknowledgement):JMS消息只有在被确认之后,才认为已经被成功地消费了...

    JMS开发例子.rar

    消息本身可以有多种类型,如TextMessage(文本消息)、ObjectMessage(对象消息)、BytesMessage(字节消息)、MapMessage(映射消息)和StreamMessage(流消息)。每种消息类型适用于不同的数据传输场景。 在JMS...

    JMSAndActiveMQ

    2. **消息类型**:JMS支持五种不同类型的消息:TextMessage(文本消息)、MapMessage(映射消息)、StreamMessage(流消息)、BytesMessage(字节消息)和ObjectMessage(对象消息),满足不同数据类型的传输需求。...

    ActiveMQ初步了解心得

    ActiveMQ支持多种消息类型,如TextMessage(文本消息)、BytesMessage(字节流消息)、MapMessage(键值对消息)和ObjectMessage(可序列化对象消息)。其中,TextMessage和ObjectMessage较为常用。 最后,JMS提供...

    jms学习指导(weblogic下的)

    - **消息体(Body)**:承载要传递的实际内容,JMS定义了多种消息类型以适应不同的数据类型,如TextMessage、ObjectMessage、BytesMessage等。 **WebLogic JMS Server** WebLogic Server 8.1是一个符合Java规范的...

    ActiveMQ in Action 中文

    JMS消息由消息头、消息属性和消息体组成,JMS定义了TextMessage、MapMessage、BytesMessage、StreamMessage和ObjectMessage等消息类型。消息的确认机制有三种模式:Session.AUTO_ACKNOWLEDGE、Session.CLIENT_...

Global site tag (gtag.js) - Google Analytics