如何发送Soap消息到activemq队列?
我正在开发一个向第三方webservice发送soap请求的项目,而且我在java开发中是一个非常新手。 到目前为止,我有一个基本的代码创建soap消息,发送它,然后接收响应; 该消息包含一个xml文件作为base64字符串文件。
我正在使用的代码正在运行,但我需要确保soap请求能够在更短的时间内成功发送消息(我有48小时的时间来保证发送),所以如果出现某种网络故障或webservice不可用我的代码会产生错误并停止程序的其余部分。
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "https://facturaelectronica.dian.gov.co/habilitacion/B2BIntegrationEngine/FacturaElectronica";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
我已经读过,可以将soap请求发送到jms qeue,这样我就可以确保整个程序在网络故障时都能正确执行,但是,对于我所知道的,我需要将soap消息转换为jms消息,我还没有弄清楚。 我发现,supposly你可以使用它来实现转换:
//Convert SOAP to JMS message.
Message m = MessageTransformer.SOAPMessageIntoJMSMessage (soapMessage,session);
根据此文档:https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html
所以我创建了一个类来调用ToJms进行转换:
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.Name;
import java.net.URL;
import javax.activation.DataHandler;
import com.sun.messaging.Queue;
import com.sun.messaging.xml.MessageTransformer;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.MessageProducer;
/**
* This example shows how to use the MessageTransformer utility to send SOAP
* messages with JMS.
* <p>
* SOAP messages are constructed with javax.xml.soap API. The messages
* are converted with MessageTransformer utility to convert SOAP to JMS
* message types. The JMS messages are then published to the JMS topics.
*/
public class ToJms {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Topic topic = null;
Queue queue = null;
MessageProducer msgProducer = null;
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
/**
* default constructor.
*/
public ToJms(String QueueName) {
init(QueueName);
}
/**
* Initialize JMS Connection/Session/Topic and Producer.
*/
public void init(String topicName) {
try {
// connectionFactory = new com.sun.messaging.ConnectionFactory();
// connection = connectionFactory.createConnection(localhost:6161);
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
msgProducer = session.createProducer(topic);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Send SOAP message with JMS API.
*/
public void send (SOAPMessage soapMessage) throws Exception {
/**
* Convert SOAP to JMS message.
*/
Message message = MessageTransformer.SOAPMessageIntoJMSMessage( soapMessage, session );
/**
* publish JMS message.
*/
msgProducer.send( message );
}
/**
* close JMS connection.
*/
public void close() throws JMSException {
connection.close();
}
/**
* The main program to send SOAP messages with JMS.
*/
public static void main (String[] args) {
String topicName = "TestTopic";
if (args.length > 0) {
topicName = args[0];
}
try {
ToJms ssm = new ToJms(topicName);
// ssm.send();
ssm.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后在我的主类上实现它:
ToJms ssm=new ToJms("SoapDian");
ssm.send(createSOAPRequest());
ssm.close();
我得到下一个错误:
线程“main”中的异常java.lang.ClassFormatError:在类文件中非本地或抽象的方法中缺少代码属性javax / xml / messaging / JAXMException java.lang.ClassLoader.defineClass1(Native Method)at java.lang。 ClassLoader.defineClass(ClassLoader.java:763)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)在java.net.URLClassLoader.defineClass(URLClassLoader.java:467)在java.net.URLClassLoader.access $ 100( URLClassLoader.java:73)在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:368)在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:362)在java.security.AccessController.doPrivileged(本机方法)在java.net.URLClassLoader.findClass(URLClassLoader.java:361)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:338)在爪哇。 lang.ClassLoader.loadClass(ClassLoader.java:357)at xml.ToJms.send(ToJms.java:111)at xml.SoapTest.main(SoapTest.java:42)at xml.xml.main(xml.java:317 ) 在com.summan.datavarprinter.Main.main(Main.java:181)
我确实有很多麻烦试图找到oracle指定的库,因为goolge上有很多不同的结果,我必须尝试很多,直到导入工作。
所以我想知道是否有人知道如何使这项工作或有更好的解决方案发送肥皂消息到activemq队列。
谢谢。
我无法找到一种方法来完成这项工作,所以我最终做的是将soap消息转换为字符串,将其发送到activemq,然后在消耗时执行反向处理。
链接地址: http://www.djcxy.com/p/33059.html上一篇: How to send Soap Message to activemq queue?
下一篇: Why reading byte array to an Object throws java.io.StreamCorruptedException?