共享一个自己编写测试过的Activemq BlobMessage Consumer 代码。

package cn.net.intervision.fileconfig;

// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   MyQueueReceiver.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.BlobMessage;
  
public class MyQueueReceiver
{
 
	  static String localdir;
	public static void main(String args[])
		throws Exception
	{
		File ivconfig = new File("IV.config");
		if(ivconfig.exists()==false){
			System.out.println("未找到配置文件IV.config,请将此文件放在Reciver.exe的同目录下。");
			usage();
			Thread.sleep(10000);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ivconfig)));
		String brokeURL = br.readLine();
		String UserName = br.readLine();
		String Password = br.readLine();
		String Queue = br.readLine();
		String LocalDir = br.readLine();
		
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokeURL);
 
		 QueueConnection con = (QueueConnection)factory.createConnection(UserName,Password);
		 con.setClientID("\""+Queue+"\"的接收端");
		   QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
	 
		javax.jms.Queue queue = session.createQueue(Queue);
		MessageConsumer consumer = session.createConsumer(queue);
		 localdir = LocalDir;
		consumer .setMessageListener(new MessageListener(){
			
			public void onMessage(Message msg) {
				BlobMessage blobmsg = (BlobMessage)msg;
				String FileName = null;
				String JMSTimestamp = null;
				String JMSExpiration = null ;
				try {
					FileName = blobmsg.getStringProperty("JMSCorrelationID");
					JMSExpiration = blobmsg.getStringProperty("JMSExpiration"); 
					JMSTimestamp = blobmsg.getStringProperty("JMSTimestamp"); 
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String FullFileName =  JMSTimestamp+"_"+JMSExpiration+"_"+FileName;
				File file = new File(localdir+"/"+FullFileName );
				try {
					file.createNewFile();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				try {
					 InputStream is = blobmsg.getInputStream();
 
					 
					 FileOutputStream fos = new FileOutputStream(file); 
					  
					 int count; 
					 byte[] buff = new byte[16 * 1024]; 
					 while((count = is.read(buff)) != -1) { 
					     fos.write(buff, 0, count); 
					 } 
					 fos.close(); 
					 is.close(); 
 
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		});
		 
	}

	private static void usage() throws IOException {
		// TODO Auto-generated method stub
		System.out.println("如不存在,新建IV.config文件于当前目录,");
		StringBuffer sb = new StringBuffer("tcp://localhost:61616\nuser\npassword\n队列1\nd:/temp\n\n\n\n\n\n\n\n\n\n");
		sb.append("注意:第一行表示连接地址,第二行为用户名,第三行密码,第四行广告队列名,第五行本地目录。首行不能为空。每行首字母不能为空。");
		 System.out.println(sb);
	 }
	} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值