Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 118 119 120 121 122 123 < 124 > 125 126 127 128 129 130 .. 166 >> Next

I'll Take the Local 341
In conclusion, use the following pieces of advice when designing network applications: ¦¦ Abstract details from the client.
¦¦ Abstract things in a multi-tiered solution.
¦¦ When the network has the potential to be a bottleneck, use the "plan and execute" strategy—it is often better to send one large message with lots of information than to send smaller messages over the network.
Item 39: I'll Take the Local
I have worked with a team that is providing EJBs available via the Web services protocols (SOAP over HTTP). They use a servlet to receive SOAP messages over HTTP. Based on the SOAP message, the appropriate EJB is referenced and called.
For the sake of clarity and simplicity, we will use a more stripped-down version to demonstrate the same concept (see Figure 39.1).
This is the source of the handler, which extends JAXMServlet and implements the ReqRespListener (a JAXM convention for handling requests that return a synchronous response).
This servlet:
1. Receives the SOAP message.
2. Parses a parameter from the SOAP message.
3. Looks up the EJBHome interface object.
4. Narrows the object into a SOAPHome object.
5. Calls create() on the SOAPHome object to create a SOAPHandler object.
6. Calls the convert() method on the SOAPHandler object.
7. Builds a response SOAP message.
Figure 39.1 Architecture overview.
342 Item 39
Listing 39.1 demonstrates this servlet.
001 /*
002 * RemoteEJBServlet.java
003 *
004 */
005
006 import javax.servlet.*;
007 import javax.servlet.http.*;
008 import javax.xml.messaging.*;
009 import javax.xml.soap.SOAPMessage;
010 import javax.xml.soap.SOAPPart;
011 import javax.xml.soap.SOAPEnvelope;
012 import javax.xml.soap.SOAPHeader;
013 import javax.xml.soap.SOAPElement;
014 import javax.xml.soap.Name;
015 import javax.xml.soap.SOAPException;
016
017 import org.xml.sax.*;
018 import org.xml.sax.helpers.*;
019 import javax.xml.parsers.*;
020
021 import org.w3c.dom.*;
022 import javax.xml.transform.*;
023 import javax.xml.transform.dom.*;
024 import javax.xml.transform.stream.*;
025
026 import java.net.URL;
027
028 import javax.naming.*;
029
030 import javax.rmi.PortableRemoteObject;
031
032 public class RemoteEJBServlet extends JAXMServlet implements 2
ReqRespListener {
033
034 /** Initializes the servlet.
035 */
036 public void init(ServletConfig config) throws ServletException {
037 super.init(config);
038
039 }
040
041 /** Destroys the servlet.
042 */
043 public void destroy() {
044
045 }
046
Listing 39.1 RemoteEJBServlet
I'll Take the Local 343
047:
048: /** Returns a short description of the servlet.
049: */
050: public String getServletInfo() {
051: return "Example of a servlet accessing an EJB through the 2
remote interface
052: }
053:
054: /** This method receives the message via SOAP, gets a Local
EJB Home interface,
055: * creates the EJB, invokes the appropriate method on the
EJB, and sends
056: * the response in a SOAP message.
057: */
058: public SOAPMessage onMessage(SOAPMessage message) {
059:
060: SOAPMessage respMessage = null;
061:
062: try {
063: //retrieve the number element from the message
064: SOAPBody sentSB = message.getSOAPPart() 2
.getEnvelope().getBody();
065: Iterator it = sentSB.getChildElements();
066: SOAPBodyElement sentSBE = (SOAPBodyElement)it.next()
067: Iterator belt = sentSBE.getChildElements();
068: SOAPElement sentSE = (SOAPElement) beIt.next();
069:
070: //get the text for the number element to put in response
071: String value = sentSE.getValue();
072:
073: Context ctx = new InitialContext();
074:
075: Object obj = ctx.lookup("mySOAPHome");
076:
077: SOAPHome obj = (SOAPHome) PortableRemoteObject.narrow 2
(obj, SOAPHome.class);
078:
079: SOAPHandler soapHandler = soapHm.create();
080:
081: String responseData = soapHandler.convert(value);
082:
083: //create the response message
084: respMessage = fac.createMessage();
085: SOAPPart sp = respMessage.getSOAPPart();
086: SOAPEnvelope env = sp.getEnvelope();
087: SOAPBody sb = env.getBody();
088: Name newBodyName = env.createName("response",
089: "return", "http://www.javapitfalls.org");
090: SOAPBodyElement response =
Listing 39.1 (continued)
344 Item 39
091 sb.addBodyElement(newBodyName);
092
093 //create the orderID element for confirmation
094 Name thingName = env.createName("thing");
095 SOAPElement newThing =
096 response.addChildElement(thingName);
097 newThing.addTextNode(responseData);
098
099 respMessage.saveChanges();
100
101 } catch (SOAPException soapE) {
102 // log this problem
103 soapE.printStackTrace();
104 }
105
106 return respMessage;
Previous << 1 .. 118 119 120 121 122 123 < 124 > 125 126 127 128 129 130 .. 166 >> Next