SimpleActiveMQUtil

import java.io.File;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.blob.BlobTransferPolicy;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleActiveMQUtil {
 public static String bindAddress = "tcp://localhost:61616";
 private static final Log log = LogFactory.getLog(SimpleActiveMQUtil.class);
 public static Connection connection;
 private static boolean useTopic;
 private static String queueName="queue";

 private static String destDirPath="/";

 private static ConnectionFactory createConnectionFactory() throws Exception {
  return new ActiveMQConnectionFactory(bindAddress);
 }

 public static Connection createConnection() throws Exception {
  return connection = createConnectionFactory().createConnection();
 }

 public static void startConnection() throws Exception {
  if (connection == null) {
   connection = createConnection();
  }
  connection.start();
 }

 public static void stopConnection() throws Exception {
  if (connection != null) {
   connection.stop();
  }
 }

 public static MessageModel receiveMessage() throws Exception {
  MessageModel mm = null;
  try {
   startConnection();
   ActiveMQSession session = (ActiveMQSession) connection
     .createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer consumer = session
     .createConsumer(createDestination());
   Message message = consumer.receive();
   if (message instanceof ActiveMQBlobMessage) {
    mm = receiveBlobMessage((ActiveMQBlobMessage) message);
   }

   if (mm == null) {
    throw new Exception("can not fond message type.");
   }
  } finally {
   stopConnection();
  }

  return mm;
 }

 /**
  * @param mm
  * @param ftpUrl
  * eg: ftp://username:password@hosts:port/workpath/
  * @throws Exception
  */
 public static void sendBlobMessage(MessageModel mm,String ftpUrl) throws Exception {
  try {
   startConnection();
   ActiveMQSession session = (ActiveMQSession) connection
     .createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = session.createQueue(queueName);
   MessageProducer producer = session.createProducer(destination);
   BlobTransferPolicy bt = session.getBlobTransferPolicy();
   bt.setBrokerUploadUrl(ftpUrl);
   BlobMessage blobMessage = session.createBlobMessage(mm
     .getMessageFile());
   producer.send(blobMessage);
   
  } finally {
   stopConnection();
  }

 }

 private static MessageModel receiveBlobMessage(ActiveMQBlobMessage message)
   throws Exception {
  String fileName = message.getMessageId().toString().replace(":", "_");
  MessageModel mm = new MessageModel();
  mm.setMessageFile(new File(FileUtils.outPutFile(
    message.getInputStream(), destDirPath, fileName)));
  return mm;
 }

 public static void main(String[] args) {
  try {

   //startBroker();
   log.info("成功启动borker。");
   MessageModel mm=new MessageModel();
   String pathname="E://820-4606.pdf";
   mm.setMessageFile(new File(pathname));
     String ftpUrl="ftp://root:jjhtmz@10.200.60.44/mytemp/";
     //设置队列名
     setQueueName("myqueue");
       sendBlobMessage(mm, ftpUrl);
   
    log.info("成功发送message.");
   
      MessageModel receive= receiveMessage();
      log.info("成功接受message["+receive.getMessageFile().getAbsolutePath()+"]");
   
     
   
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public static ActiveMQDestination createDestination() {
  return createDestination(getDestinationString());
 }

 public static ActiveMQDestination createDestination(String subject) {
  // TODO Auto-generated method stub
  if (useTopic) {
   return new ActiveMQTopic(subject);
  } else {
   return new ActiveMQQueue(subject);
  }
 }

 public static String getDestinationString() {
  return getQueueName();
 }

 public static void startBroker() throws Exception {
  BrokerService service = new BrokerService();
  service.addConnector(bindAddress);
  service.start();
 }

 public static String getBindAddress() {
  return bindAddress;
 }

 public static void setBindAddress(String bindAddress) {
  SimpleActiveMQUtil.bindAddress = bindAddress;
 }

 public static boolean isUseTopic() {
  return useTopic;
 }

 public static void setUseTopic(boolean useTopic) {
  SimpleActiveMQUtil.useTopic = useTopic;
 }

 public static String getQueueName() {
  return queueName;
 }

 public static void setQueueName(String queueName) {
  SimpleActiveMQUtil.queueName = queueName;
 }

 public static String getDestDirPath() {
  return destDirPath;
 }

 public static void setDestDirPath(String destDirPath) {
  SimpleActiveMQUtil.destDirPath = destDirPath;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值