1

I'm trying to turn a Java servlet into a Mule web service, but my program has more than one class. I've seen many tutorials about using POJOs with Mule in a Java component, but never a program with several classes. How can this be done?

Edit: My servlet is currently running on a tomcat server. It takes in an xml document with details for a search, searches a database, then outputs an xml document with the search results. The xml parsing and generation, and the database connection and queries are all handled by the servlet currently. I'd just like to be able to run it using Mule, rather than on the tomcat server.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
Carasel
  • 2,740
  • 5
  • 32
  • 51

1 Answers1

3

Mule can run JavaEE web-apps thanks to its embedded Jetty container.

Look at the Bookstore example that comes bundled with the standalone distribution to see how it is done. This example indeed does deploy two web-apps, bookstore and bookstore-admin inside Mule standalone.

Assuming your web-app is xmlproc.war, the layout you want in your Mule application Zip is:

.
├── mule-config.xml
├── classes
│   ├── <classes and resources from xmlproc/WEB-INF/classes>
├── lib
│   ├── <non-provided libs from xmlproc/WEB-INF/lib>
└── webapps
    └── xmlproc
        ├── <jsps>
        └── WEB-INF
            └── web.xml

With mule-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jetty="http://www.mulesoft.org/schema/mule/jetty"
      xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/jetty http://www.mulesoft.org/schema/mule/jetty/current/mule-jetty.xsd">

    <jetty:connector name="jettyConnector">
        <jetty:webapps directory="${app.home}/webapps" port="8083"/>
    </jetty:connector>
</mule>

Non-provided libs means you shouldn't embed libs that are found under $MULE_HOME/lib/**.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Thanks David. How do I add a dependency? I searched online but can only find info about Maven. – Carasel Jan 22 '13 at 08:52
  • Are you using Maven? If yes, just add your app as a dependency. From what you're saying, it seems you want to invoke a `main()` method from your component, which is of course feasible: just call it as a static call. – David Dossot Jan 22 '13 at 18:24
  • I'm not using Maven. I could if it is necessary, but would prefer to keep it as simple as possible. And it's a servlet, not an application with a main method. Basically I would like my servlet to run using Mule, similar to how it currently runs on a tomcat server. Is that reasonable? (more info about servlet added in question) – Carasel Jan 23 '13 at 13:48
  • Changed my answer since you basically changed the question :D And yes, this is totally feasible. – David Dossot Jan 23 '13 at 16:22
  • Yeah, I realised there was a much better way to explain it. Thanks, I'll take a look at the bookstore example. – Carasel Jan 23 '13 at 16:48
  • From that example it looks like I was along the right lines initially. I have an http endpoint, followed by a soap component set up as a simple service, followed by a java component which has my controller class as its object as that is the class containing my doPost method and initialisation. This gives no errors in Mule studio, but trying to connect to it using a browser gives a soap error "No such operation: (HTTP GET PATH_INFO: /)" And I can't work out how to input an xml file to the mule application either. Would that be a file endpoint? – Carasel Jan 24 '13 at 15:30
  • Wait, I'm lost: you now want to call the Servlet from a Mule flow, not host it as-is in a web-app? If that is the case, you will have to adapt the Mule HTTP request into a Servlet request to be able to call doPost. In any case, can you please open a new question showing *exactly* what you want to achieve and what you've tried so far? – David Dossot Jan 24 '13 at 17:00
  • The situation is still as I asked in the question edit. When I looked at the bookstore example it looked like what I want to do is most similar to the customer side of the bookstore, which looked like it was done using the http server, soap component and java component, but I guess I must have been looking at the wrong part. I don't really know what else to tell you - I've never used Mule before and have just been told to make this servlet work with Mule instead of tomcat. – Carasel Jan 24 '13 at 19:59
  • 1
    I've added a detailed explanation of how the Mule app should look like when you wrap your existing web-app as a Mule app. I hope this helps. As you see the Mule config is just a "shell" that delegates to Jetty to load the web.xml and then run the web-app as is it was deployed on any other Java web container. – David Dossot Jan 24 '13 at 22:04
  • Ah, that's great, thanks, now I see what you meant me to look at in the bookstore example. I'm running into another problem with this though - it's coming up with the error "No such webapps resource file" when I run the app, and when I check the location it's correct - the file is not there, but it is there until I try to run the app, I've tried copying the file back in, but it just disappears again. – Carasel Jan 25 '13 at 11:34
  • Can you please open a new question, showing the layout of your app, the path of the file that disappears, the full exception stacktrace and the Mule version you're running. – David Dossot Jan 25 '13 at 17:04