You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by manish_goyal <ma...@infosys.com> on 2008/11/10 08:37:02 UTC

Can we send a JAVA object to ActiveMQ JMS Queue

Hi,

I am using Java application to send a JAVA Object to ActiveMQ JMS queue. 
Is this possible to send ?

I m using following method to send JAVA Object but it's not accepting JAVA
object.
producer.send(session.createObjectMessage(java_object));

Please clarify this issue ASAP or give some pointer to solve this issue.

Thanks.

Regards,
Manish 

-- 
View this message in context: http://www.nabble.com/Can-we-send-a-JAVA-object-to-ActiveMQ-JMS-Queue-tp20415514p20415514.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Can we send a JAVA object to ActiveMQ JMS Queue

Posted by JimmyYang <ya...@gmail.com>.
 yes.
  but you need to implement the serializable interface for the object which
is sent to consumer.
  code example:
 
 /*the object you will be sent to consumer*/
  public class MSObject implements java.io.Serializable{
  ......
  }

  /*sending method*/
  private void sendObjectMessage() throws Exception {
		ActiveMQSession mysession = (ActiveMQSession) session;
		ObjectMessage msg = session.createObjectMessage();
		msg.setObject( new MSObject("first name","Jimmy") );
		msg.setJMSType(this.CMD_TYPE_MESSAGETYPE_OBJECT);
		producer.send(msg);
		if (transacted) {
			session.commit();
		}
		System.out.println("The object message has been sent to " + this.subject
				+ " queue.");
		Thread.sleep(sleepTime);
	}
    /*receiving method
    public void onMessage(Message message) {
    ......
        if (message instanceof ObjectMessage) {
		System.out.println("recevied a Object Message");
		ObjectMessage msg = (ObjectMessage)message;
		MSObject obj = (MSObject)msg.getObject();
		System.out.println("info: ");
		System.out.println("key: "+obj.getKey());
		System.out.println("value: "+obj.getValue());
	}
    }
        FYI,good luck! :)


manish_goyal wrote:
> 
> Hi,
> 
> I am using Java application to send a JAVA Object to ActiveMQ JMS queue. 
> Is this possible to send ?
> 
> I m using following method to send JAVA Object but it's not accepting JAVA
> object.
> producer.send(session.createObjectMessage(java_object));
> 
> Please clarify this issue ASAP or give some pointer to solve this issue.
> 
> Thanks.
> 
> Regards,
> Manish 
> 
> 

-- 
View this message in context: http://www.nabble.com/Can-we-send-a-JAVA-object-to-ActiveMQ-JMS-Queue-tp20415514p20435476.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Can we send a JAVA object to ActiveMQ JMS Queue

Posted by Hans Bausewein <ha...@comerwell.xs4all.nl>.

manish_goyal wrote:
> 
> Hi,
> 
> I am using Java application to send a JAVA Object to ActiveMQ JMS queue. 
> Is this possible to send ?
> 
> I m using following method to send JAVA Object but it's not accepting JAVA
> object.
> producer.send(session.createObjectMessage(java_object));
> 

Is your object Serializable?

What exception do you get?

See Javadoc of "javax.jms.ObjectMessage":
http://java.sun.com/j2ee/1.4/docs/api/

An ObjectMessage object is used to send a message that contains a
serializable object in the Java programming language ("Java object"). It
inherits from the Message interface and adds a body containing a single
reference to an object. Only Serializable Java objects can be used.

If a collection of Java objects must be sent, one of the Collection classes
provided since JDK 1.2 can be used.

When a client receives an ObjectMessage, it is in read-only mode. If a
client attempts to write to the message at this point, a
MessageNotWriteableException is thrown. If clearBody is called, the message
can now be both read from and written to.

============
Of course, you must also consume the message as a Java Object.

Hans

-- 
View this message in context: http://www.nabble.com/Can-we-send-a-JAVA-object-to-ActiveMQ-JMS-Queue-tp20415514p20415969.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.