如何将 Soap 消息发送到 activemq 队列?
How to send Soap Message to activemq queue?
我正在从事一个向第三方网络服务发送 soap 请求的项目,我是 java 开发的新手。到目前为止,我有一个创建 soap 消息、发送它然后接收响应的基本代码;该消息包含一个 xml 文件作为 base64 字符串文件。
我正在使用的代码正在运行,但我需要确保 soap 请求能够在更短的时间内成功传递消息(我有 48 小时来保证传递)所以如果有某种网络故障或网络服务不可用我的代码会产生错误并停止程序的其余部分。
// 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 队列,这样我可以确保整个程序在网络故障的情况下仍能正确执行,但是,据我所知,我需要转换soap 消息到 jms 消息,我还没有弄清楚。我发现你可以用它来实现转换:
//Convert SOAP to JMS message.
Message m = MessageTransformer.SOAPMessageIntoJMSMessage (soapMessage,session);
基于此文档:
https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html
所以我创建了一个 class 来调用 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();
}
}
}
然后在我的主 class:
上实现它
ToJms ssm=new ToJms("SoapDian");
ssm.send(createSOAPRequest());
ssm.close();
我收到下一个错误:
Exception in thread "main" java.lang.ClassFormatError: Absent Code
attribute in method that is not native or abstract in class file
javax/xml/messaging/JAXMException at
java.lang.ClassLoader.defineClass1(Native Method) at
java.lang.ClassLoader.defineClass(ClassLoader.java:763) at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at
java.net.URLClassLoader.access0(URLClassLoader.java:73) at
java.net.URLClassLoader.run(URLClassLoader.java:368) at
java.net.URLClassLoader.run(URLClassLoader.java:362) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:361) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at
java.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) at
com.summan.datavarprinter.Main.main(Main.java:181)
我在尝试查找 oracle 指定的库时遇到了很多麻烦,因为在 goolge 上有很多不同的结果,我必须尝试很多直到导入成功。
所以我想知道这里是否有人知道如何进行这项工作或者有更好的解决方案来将 soap 消息发送到 activemq 队列。
谢谢。
我找不到实现该功能的方法,所以我最终做的是将 soap 消息转换为字符串,将其发送到 activemq,然后在使用时执行逆过程。
我正在从事一个向第三方网络服务发送 soap 请求的项目,我是 java 开发的新手。到目前为止,我有一个创建 soap 消息、发送它然后接收响应的基本代码;该消息包含一个 xml 文件作为 base64 字符串文件。
我正在使用的代码正在运行,但我需要确保 soap 请求能够在更短的时间内成功传递消息(我有 48 小时来保证传递)所以如果有某种网络故障或网络服务不可用我的代码会产生错误并停止程序的其余部分。
// 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 队列,这样我可以确保整个程序在网络故障的情况下仍能正确执行,但是,据我所知,我需要转换soap 消息到 jms 消息,我还没有弄清楚。我发现你可以用它来实现转换:
//Convert SOAP to JMS message.
Message m = MessageTransformer.SOAPMessageIntoJMSMessage (soapMessage,session);
基于此文档: https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html
所以我创建了一个 class 来调用 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();
}
}
}
然后在我的主 class:
上实现它 ToJms ssm=new ToJms("SoapDian");
ssm.send(createSOAPRequest());
ssm.close();
我收到下一个错误:
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/xml/messaging/JAXMException at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access0(URLClassLoader.java:73) at java.net.URLClassLoader.run(URLClassLoader.java:368) at java.net.URLClassLoader.run(URLClassLoader.java:362) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:361) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.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) at com.summan.datavarprinter.Main.main(Main.java:181)
我在尝试查找 oracle 指定的库时遇到了很多麻烦,因为在 goolge 上有很多不同的结果,我必须尝试很多直到导入成功。
所以我想知道这里是否有人知道如何进行这项工作或者有更好的解决方案来将 soap 消息发送到 activemq 队列。
谢谢。
我找不到实现该功能的方法,所以我最终做的是将 soap 消息转换为字符串,将其发送到 activemq,然后在使用时执行逆过程。