Mule+Servlet+JPA

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
(checkpoint)
 
(Mule and Resin using the Mule Servlet provider and JPA (Amber): checkpoint)
Line 50: Line 50:
 
</pre>
 
</pre>
 
</li>
 
</li>
 +
 +
<li>Create the configuration file <code>/usr/local/share/resin/webapps/mule/WEB-INF/classes/persistence.xml</code>
 +
<pre><?xml version="1.0" encoding="UTF-8"?>
 +
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
 +
  <persistence-unit name="example">
 +
    <class>example.CourseBean</class>
 +
 +
    <exclude-unlisted-classes/>
 +
  </persistence-unit>
 +
</persistence>
 +
</pre>
 +
</li>
 +
 
<li>Create a data bean file <code>/usr/local/share/resin/webapps/mule/WEB-INF/classes/example/CourseBean.java</code>
 
<li>Create a data bean file <code>/usr/local/share/resin/webapps/mule/WEB-INF/classes/example/CourseBean.java</code>
<pre>
+
<pre>package example;
package example;
+
  
 
import javax.persistence.*;
 
import javax.persistence.*;
Line 98: Line 110:
 
</pre>
 
</pre>
 
</li>
 
</li>
<li>Create the implementation file
+
<li>Create the Mule component
<code>/usr/local/share/resin/webapps/cxf/WEB-INF/classes/demo/StrLenImpl.java</code>
+
<code>/usr/local/share/resin/webapps/cxf/WEB-INF/classes/example/CourseComponent.java</code>
<pre>
+
<pre>package example;
package demo;
+
  
import javax.jws.WebService;
+
import java.io.*;
 +
import javax.servlet.*;
 +
import javax.servlet.http.*;
 +
import javax.persistence.*;
  
@WebService(endpointInterface="demo.StrLen")
+
import org.mule.umo.lifecycle.Callable;
public class StrLenImpl
+
import org.mule.umo.UMOEventContext;
 +
 
 +
public class CourseComponent implements Callable
 
{
 
{
   public int strlen(String x)
+
  @PersistenceContext(name="example")
 +
  private EntityManager _manager;
 +
 
 +
   public Object onCall(UMOEventContext context)
 
   {
 
   {
     return x.length();
+
     StringBuilder sb = new StringBuilder();
  }
+
}
+
</pre>
+
</li>
+
<li>Create a Servlet to access the service
+
<code>/usr/local/share/resin/webapps/cxf/WEB-INF/classes/demo/StrLenDemo.java</code>
+
<pre>
+
package demo;
+
  
import java.io.IOException;
+
    CourseBean []course = new CourseBean[2];
import java.io.Writer;
+
  
import javax.servlet.http.HttpServlet;
+
    course[0] = _manager.find(CourseBean.class, new Integer(1));
import javax.servlet.http.HttpServletRequest;
+
    course[1] = _manager.find(CourseBean.class, new Integer(2));
import javax.servlet.http.HttpServletResponse;
+
import javax.webbeans.In;
+
  
public class StrLenDemo extends HttpServlet
+
    sb.append("Course Details\n\n");
{
+
  @In StrLen strlenClient;
+
  
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
+
     for (int i = 0; i < course.length; i++) {
     throws IOException
+
      sb.append("course: " + course[i].getCourse() + "\n");
  {
+
      sb.append("teacher: " + course[i].getTeacher() + "\n\n");
    Writer out = resp.getWriter();
+
    }
    out.write("strlen(\"hello, world\") = " +  
+
 
              strlenClient.strlen("hello, world"));
+
    return sb.toString();
 
   }
 
   }
 
}
 
}
Line 143: Line 149:
 
</li>
 
</li>
 
<li>Start Resin with <code>java -jar /usr/local/share/resin/lib/resin.jar</code></li>
 
<li>Start Resin with <code>java -jar /usr/local/share/resin/lib/resin.jar</code></li>
<li>Look at http://localhost:8080/cxf/StrLenDemo
+
<li>Look at http://localhost:8080/mule/course
 
<br/>It should show  
 
<br/>It should show  
 
<pre>
 
<pre>
strlen("hello, world") = 12
+
Course Details
 +
 
 +
course: Potions
 +
teacher: Severus Snape
 +
 
 +
course: Transfiguration
 +
teacher: Minerva McGonagall
 
</pre>
 
</pre>
 
</ol>
 
</ol>

Revision as of 22:32, 12 February 2008

Mule and Resin using the Mule Servlet provider and JPA (Amber)

Mule can be run within Resin and use objects created by it as components. This allows Mule to take advantage of Resin IoC and Amber, Caucho's implementation of JPA. This example shows how to use all of these technologies together.

(The following has been tested with Resin 3.1.5 and Mule 1.4.3)

  1. Download Resin from http://www.caucho.com/download
  2. Unzip Resin into /usr/local/share/resin
  3. Create the Mule webapp directory structure:
    mkdir -p /usr/local/share/resin/webapps/mule/WEB-INF/lib/
    mkdir -p /usr/local/share/resin/webapps/mule/WEB-INF/classes/example/
    mkdir -p /usr/local/share/resin/webapps/mule/WEB-INF/classes/META-INF/
  4. Download Mule from http://mule.mulesource.org/display/MULE/Download
  5. Unzip Mule
  6. Copy all the jars included the Mule lib/ directory to
    /usr/local/share/resin/webapps/mule/WEB-INF/lib/
  7. Create the configuration file /usr/local/share/resin/webapps/mule/WEB-INF/resin-web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://caucho.com/ns/resin">
      <database>
        <jndi-name>jdbc/resin</jndi-name>
        <driver type="org.gjt.mm.mysql.Driver">
          <url>jdbc:mysql://localhost:3306/MuleJPA</url>
          <user>root</user>
          <password/>
        </driver>
      </database>
    
      <ejb-server data-source="jdbc/resin"/>
    
      <bean class="example.CourseComponent" name="courseComponent"/>
    
      <context-param>
        <param-name>org.mule.config</param-name>
        <param-value>/WEB-INF/course-config.xml</param-value>
      </context-param>
    
      <listener>
        <listener-class>com.caucho.mule.MuleResinContextListener</listener-class>
      </listener>
    
      <servlet servlet-name="mule-servlet"
               servlet-class="org.mule.providers.http.servlet.MuleReceiverServlet"/>
    
      <servlet-mapping url-pattern="/*" servlet-name="mule-servlet"/>
    </web-app>
    
  8. Create the configuration file /usr/local/share/resin/webapps/mule/WEB-INF/classes/persistence.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
      <persistence-unit name="example">
        <class>example.CourseBean</class>
    
        <exclude-unlisted-classes/>
      </persistence-unit>
    </persistence>
    
  9. Create a data bean file /usr/local/share/resin/webapps/mule/WEB-INF/classes/example/CourseBean.java
    package example;
    
    import javax.persistence.*;
    
    @Entity@Table(name="basic_courses")
    public class CourseBean {
      private int _id;
      private String _course;
      private String _teacher;
    
      @Id@Column(name="id")
      @GeneratedValue
      public int getId()
      {
        return _id;
      }
    
      public void setId(int id)
      {
        _id = id;
      }
    
      @Basic
      public String getCourse()
      {
        return _course;
      }
    
      public void setCourse(String course)
      {
        _course = course;
      }
    
      @Basic
      public String getTeacher()
      {
        return _teacher;
      }
    
      public void setTeacher(String teacher)
      {
        _teacher = teacher;
      }
    }
    
  10. Create the Mule component /usr/local/share/resin/webapps/cxf/WEB-INF/classes/example/CourseComponent.java
    package example;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.persistence.*;
    
    import org.mule.umo.lifecycle.Callable;
    import org.mule.umo.UMOEventContext;
    
    public class CourseComponent implements Callable
    {
      @PersistenceContext(name="example")
      private EntityManager _manager;
    
      public Object onCall(UMOEventContext context)
      {
        StringBuilder sb = new StringBuilder();
    
        CourseBean []course = new CourseBean[2];
    
        course[0] = _manager.find(CourseBean.class, new Integer(1));
        course[1] = _manager.find(CourseBean.class, new Integer(2));
    
        sb.append("Course Details\n\n");
    
        for (int i = 0; i < course.length; i++) {
          sb.append("course: " + course[i].getCourse() + "\n");
          sb.append("teacher: " + course[i].getTeacher() + "\n\n");
        }
    
        return sb.toString();
      }
    }
    
  11. Start Resin with java -jar /usr/local/share/resin/lib/resin.jar
  12. Look at http://localhost:8080/mule/course
    It should show
    Course Details
    
    course: Potions
    teacher: Severus Snape
    
    course: Transfiguration
    teacher: Minerva McGonagall
    
Personal tools