1 回答
TA贡献1712条经验 获得超3个赞
在 JMS(以及一般的消息传递)中,您发送的是消息而不是文件。您可以通过从文件中读取数据并将其放入 JMS 文本或字节消息中来将文件转换为消息。最简单的解决方案是这样的:
public class Main {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "TestQueue";
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageProducer producer = session.createProducer(destination);
String xml = readXmlFromFile();
TextMessage message = session.createTextMessage(xml);
producer.send(message);
connection.close();
}
private static String readXmlFromFile() {
// read XML from file
}
}
添加回答
举报