小记录一下:ActiveMQ send blobmessage

 

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

 

谢谢 templexp 了

 

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

 

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

 

A problem with blobmessage on activemq 5.0

View: <script> Nabble.selectOption(Nabble.get("nabble.viewSelect"),Nabble.tview); </script> New views
2 Messages — Rating Filter:   Alert me   <script> if( Nabble.user == 978669 || Nabble.user == 792292) { document.writeln('

A problem with blobmessage on activemq 5.0

Click to flag this post <script>document.write(Nabble.ratingStars(3));</script>

by <script>document.write('');</script> Iósev Pérez Rivero <script>document.write('');</script> <script>document.write(Nabble.formatDateLong(new Date(1203201553000)));</script> Feb 17, 2008; 06:39am :: Rate this Message: - Use ratings to moderate (?)

<script> document.writeln('Reply | '); </script> Reply | Reply to Author <script> if( Nabble.user == 978669 ) { document.writeln('| Delete'); } else if( Nabble.user == 792292 ) { document.writeln('| Delete'); } document.writeln('| Print'); </script> | 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
 
<script> if (Nabble.searchterms != null && Nabble.searchterms.length > 0) { Nabble.hilt(Nabble.searchterms, Nabble.get("nabble.msgtxt15523989")); } </script>

Re: A problem with blobmessage on activemq 5.0

Click to flag this post <script>document.write(Nabble.ratingStars(3));</script>

by <script>document.write('');</script> Marco Buss <script>document.write('');</script> <script>document.write(Nabble.formatDateLong(new Date(1211382124608)));</script> May 21, 2008; 11:02pm :: Rate this Message: - Use ratings to moderate (?)

<script> document.writeln('Reply | '); </script> Reply | Reply to Author <script> if( Nabble.user == 826206 ) { document.writeln('| Edit'); document.writeln('| Delete'); } else if( Nabble.user == 792292 ) { document.writeln('| Delete'); } document.writeln('| Print'); </script> | 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);
 }
}
<script> if (Nabble.searchterms != null && Nabble.searchterms.length > 0) { Nabble.hilt(Nabble.searchterms, Nabble.get("nabble.msgtxt17364301")); } </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值