现在的位置: 首页 > 综合 > 正文

小记录一下:ActiveMQ send blobmessage

2013年09月07日 ⁄ 综合 ⁄ 共 6568字 ⁄ 字号 评论关闭

 

 网上的朋友问起为什么发送 blobmessage 的时候出现错误,解决了顺便记录下,以后用的时候也可以翻录下了,

 

谢谢 templexp 了

 

再次证明了开源的特性,再次证明了 nabble ,wiki 的可参考行。

 

开源啊,不用还真对不起咱这张脸。

 

A problem with blobmessage on activemq 5.0

View:

New views

2 Messages — Rating Filter:

  Alert me  

A problem with blobmessage on activemq 5.0

Click to flag this post

by
Iósev Pérez Rivero


Feb 17, 2008; 06:39am
:: Rate this Message: - Use ratings to moderate (?)

Reply | Reply to Author

| Print | View Threaded | Show Only this Message

I have a problem with this code:
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQQueue;
public class Sending {
 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  long a1 = System.currentTimeMillis();
  ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
    "tcp://localhost:61616");
  ActiveMQConnection conn = null;
  ActiveMQSession session = null;
  try {
   conn = (ActiveMQConnection)cf.createConnection();
   session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = new ActiveMQQueue("Queue");
   MessageProducer producer = session.createProducer(destination);
   
   BlobMessage message = session.createBlobMessage(new File("src//BlobMessage//file//foto.jpg"));
   
   System.out.println(message.getInputStream());
   
   producer.send(message);
  } catch (JMSException e) {
   e.printStackTrace();
  } finally {
   try {
    if (session != null) {
     session.close();
    }
    if (conn != null) {
     conn.close();
    }
   } catch (JMSException ex) {
   }
  }
  a1 = System.currentTimeMillis() - a1;
  System.out.println(a1);
 }
}
 
When i create blobmessage object and print his inputstream pirnted NULL, why??? then when i send the message the file not sending.
 
Please may anybody help me??
 
Ing. Iósev Pérez Rivero
Dirección Técnica - IP
Universidad de las Ciencias Informaticas
 

Re: A problem with blobmessage on activemq 5.0

Click to flag this post

by
Marco Buss


May 21, 2008; 11:02pm
:: Rate this Message: - Use ratings to moderate (?)

Reply | Reply to Author

| Print | View Threaded | Show Only this Message

Hello Iósev,

i have take a look on your code an make a few changes so the Blob functionality will work. The easyest way for you is to use the latest stable release 5.1.0 because in the former versions there is a bug, so that the fileserver webapp didn`t work out of the box.
see: https://issues.apache.org/activemq/browse/AMQ-1624

Marco Buss

=========== snip ========================
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQQueue;

public class Sending {
 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  long a1 = System.currentTimeMillis();
 
  /*
   * First you must tell how the Blob repository can be found.
   * Use ActiveMQ 5.1.0+ as broker because the fileserver webapp works there out of the box.
   */
  ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
    "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8161/fileserver/");
  ActiveMQConnection conn = null;
  ActiveMQSession session = null;
  try {
   conn = (ActiveMQConnection)cf.createConnection();
   session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = new ActiveMQQueue("Queue");
   MessageProducer producer = session.createProducer(destination);
   
   /*
    * !!!!!!!!!!!!!!!!!!!!!!!!!
    * very important. If it is set to true (default) the uploader is lost in translation ;)
    * !!!!!!!!!!!!!!!!!!!!!!!!!
    */
   conn.setCopyMessageOnSend(false);
   
   File file = File.createTempFile("amq-data-file-", ".dat");
   // lets write some data
   BufferedWriter writer = new BufferedWriter(new FileWriter(file));
   writer.append("Hello World!");
   writer.close();
   
   /*
    * i have used an simple ascii file instead of the picture you want to use
    */
   BlobMessage message = session.createBlobMessage(file);
   
   /*
    * should only work if you receive the message
    */
   //System.out.println(message.getInputStream());
   
   producer.send(message);
   
   /*
    * After all you can see a new file under $Brokerlocation$/webapps/fileserver/
    */
  } catch (JMSException e) {
   e.printStackTrace();
  } finally {
   try {
    if (session != null) {
     session.close();
    }
    if (conn != null) {
     conn.close();
    }
   } catch (JMSException ex) {
   }
  }
  a1 = System.currentTimeMillis() - a1;
  System.out.println(a1);
 }
}

抱歉!评论已关闭.