- package com.work.activemq;
- /**
- * @author wangmingjie
- * @date 2009-7-29上午09:00:48
- */
- import java.io.File;
- import java.io.IOException;
- import javax.jms.DeliveryMode;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MessageProducer;
- 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.ActiveMQQueue;
- public class BlobMessageSendTest {
- private String user = ActiveMQConnection.DEFAULT_USER;
- private String password = ActiveMQConnection.DEFAULT_PASSWORD;
- // private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
- private String url = "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8161/fileserver/";
- private String subject = "Blob Queue";
- private Destination destination = null;
- private ActiveMQConnection connection = null;
- private ActiveMQSession session = null;
- private MessageProducer producer = null;
- // 初始化
- private void initialize() throws JMSException, Exception {
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
- connection = (ActiveMQConnection)connectionFactory.createConnection();
- /*
- * !!!!!!!!!!!!!!!!!!!!!!!!! very important. If it is set to true
- * (default) the uploader is lost in translation ;)
- * !!!!!!!!!!!!!!!!!!!!!!!!!
- */
- connection.setCopyMessageOnSend(false);
- session = (ActiveMQSession)connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- destination = session.createQueue(subject);
- // destination = session.createTopic(subject);
- producer = session.createProducer(destination);
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- }
- // 发送消息
- public void produceMessage(File file) throws JMSException, Exception {
- initialize();
- BlobMessage msg = session.createBlobMessage(file);
- connection.start();
- System.out.println("Producer:->Sending message: " + file.getName());
- producer.send(msg);
- System.out.println("Producer:->Message sent complete!");
- }
- // 关闭连接
- public void close() throws JMSException {
- System.out.println("Producer:->Closing connection");
- if (producer != null)
- producer.close();
- if (session != null)
- session.close();
- if (connection != null)
- connection.close();
- }
- /**
- * 测试成功,能够发送到服务器上。
- * @param args
- * @throws IOException
- */