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;
}
}