3. RMI vi aSMTPThe configuration details given in the preceding chapter also apply to the SMTP transport Switch RMI implements Remote Method Invocation via SMTP as an "aynchronous" or "one-way" message transmission. The method invocation is packaged up using the chosen RMI protocol and shipped as a MIME attachment to a destination email address.and the client call returns immediately. The client does not wait for a return value from the remote object. RMI via SMTP is useful when no immediate response is required or when numerous remote locations must receive a notification. It is possible to configure a client and send messages to your own mailbox and preview the RMI request as an email attachment in your mail reader program, especially if a XML or text based RMI protocol, like XMLRPXC, is used. Publishing an Object
The This service class can use pop3 or imap to contact the mailbox using the JavaMail providers shipped by Sun Microsystems with the JavaMail implementation. Usernames and passwords as well as pop3 or imap hosts must be supplied via the appropriate JavaMail system properties:
The Invoking Methods via SMTP from your ApplicationThe API for RMI via SMTP is almost exactly the same as that demonstrated in the previous chapter for the HTTP transport. The URL format for SMTP is more complicated than for HTTP. The URL must contain all of the information necessary to connect to an SMTP server, with authorization if necessary, and send email to an email address The example below, shows that the host portion of the URL contains the name of the SMTP host while the URL query parameters contain the username and password if the SMTP server requires authentication. The "from" and "to" values supply the email addresses for the SMTP submission. If the SMTP server does not require authentication the "user" and "password" values can be left off the URL query parameters. As in the case of the HTTP URL the path portion of the URL contains the object name and the RMI protocol name. smtp:smtp.host/rmi/echo.xmlrpc?from=p1@c1.com&to=p2@c2.com&user=smtp.user&password=smtp.password As described for the HTTP transport in the previous chapter, obtain a RMIClient for the SMTP transport and then use the client to connect to the remote object via a proxy: import com.mjh.switchrmi.*; private RmiClient rmi; private IEcho proxy; try { rmi = new RmiClient(); proxy = rmi.connect("smtp:smtp.host/rmi/echo.xmlrpc?from=one@company.com&to=another@anotherco.com", IEcho.class); } catch (Exception ex) { } Once you have successfully obtained a proxy to the remote object you can then invoke the methods from the requested interface on the proxy: try { String string = "this is a string"; proxy.echo(string); } catch (Exception ex) { } |