title: How to send Soap Message to activemq queue?
body: <p>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. </p>
  
  <p>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. </p>
  
  <pre><code>// 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();
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> //Convert SOAP to JMS message.
      Message m = MessageTransformer.SOAPMessageIntoJMSMessage                                                    (soapMessage,session);
  </code></pre>
  
  <p>base on this documentation:
  <a href="https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html" rel="nofollow noreferrer">https://docs.oracle.com/cd/E19340-01/820-6767/aeqgk/index.html</a>  </p>
  
  <p>so I created a class to call ToJms to make the conversion:</p>
  
  <pre><code>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.
   * &lt;p&gt;
   * 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 &gt; 0) {
              topicName = args[0];
          }
          try {
              ToJms ssm = new ToJms(topicName);
             // ssm.send();
              ssm.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>and then implemeted it on my main class:</p>
  
  <pre><code>    ToJms ssm=new ToJms("SoapDian");
      ssm.send(createSOAPRequest());
      ssm.close();
  </code></pre>
  
  <p>And I'm getting the next error:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks.</p>