How to send Soap Message to activemq queue?

I'm working on a project that sends soap request to a third party webservice, and I'm a very newbie at java development. So far I have a basic code that creates the soap message, sends it and then receive the response; the message contains an xml file as a base64 string file.

The code I'm using is working but I need to ensure that the soap request would successfully deliver the message in the less amount of time, (I have 48 hours to guarantee the delivering) so if there is some kind of network failure or the webservice is not available my code would produce an error and stop the rest of the program.

// 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();

I've read that is possible to send the soap request to a jms qeue and that way I would ensure that the whole program would execute correctly despite network failures, but, for what I know, I need to transform the soap message to a jms message and I haven't been able to figure that out yet. I found that supposly you can use this to achieve the conversion:

 //Convert SOAP to JMS message.
    Message m = MessageTransformer.SOAPMessageIntoJMSMessage                                                    (soapMessage,session);

base on this documentation: https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html

so I created a class to call ToJms to make the conversion:

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

and then implemeted it on my main class:

    ToJms ssm=new ToJms("SoapDian");
    ssm.send(createSOAPRequest());
    ssm.close();

And I'm getting the next error:

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.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:368) at java.net.URLClassLoader$1.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)

I did have many troubles trying to find the libraries specified by oracle because there was a lot of different results on goolge for that and I have to try many until the imports worked.

So I was wondering if anyone here knows how to make this work or has a better solution to send the soap messages to the activemq queue.

Thanks.


我无法找到一种方法来完成这项工作,所以我最终做的是将soap消息转换为字符串,将其发送到activemq,然后在消耗时执行反向处理。

链接地址: http://www.djcxy.com/p/33060.html

上一篇: Laravel 5 database.php设置为sqlite

下一篇: 如何发送Soap消息到activemq队列?