Remoting Plugin
If you have ever wanted to access your Grails services via RMI or some other remote protocol, this is your plugin. Grails services can be exposed via the following protocols:
- RMI
- Hessian
- Burlap
- Spring's HttpInvoker (Java serialisation over HTTP)
Note The plugin only works with Grails 0.5 and above.
Setup
1. Install the plugin
For Grails 0.5: grails install-plugin Remoting 0.2 For Grails 0.5.5+: grails install-plugin remoting
If you run Grails from your-IDE, you must also compile the plugin's Java sources:
grails dev package
2.1 Exporting your service
There are two aspects to configuring a service for remoting: first, the service must implement an interface - this interface defines the methods that will be available via the remoting protocol.
Second, add a static property called expose and set it to a list of the protocols that you want to expose the service via. For example,
class SecurityService implements example.SecurityService { static expose = ['hessian', 'rmi'] def authenticate(username, password) { ... } def isPermitted(user, permission) { ... } }
expose can accept any of these values: 'rmi', 'hessian', 'burlap', and 'httpinvoker'.
| XFire Compatibility You may notice that the configuration is similar to that for the XFire web services plugin. In fact, you should be able to install that plugin and add 'xfire' to the expose list: static expose = ['xfire', 'rmi', 'hessian']
|
2.2. Update your UrlMappings file
Note that this step only applies to Grails 0.5 - versions >= 0.5.6 do not have the problem
Unfortunately, Grails will currently try to process all the remoting URLs itself if you use the default URL mapping file. A simple fix is to add an extra element at the root of the URL path:
class UrlMappings.groovy {
static mappings = {
"/ui/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
}
}
Alternatively, you can manually map each of your controllers separately.
3. Access a remote service
You can easily access a remote service from your Grails application by creating a normal Grails service and adding a static remote property:
class SecurityService {
static remote = [
protocol: 'hessian',
iface: example.SecurityService,
host: 'castle',
port: '9090',
webcontext: 'security',
]
}
This service will be injected into your controllers and other services. Any method calls on the service will be transparently routed to the remote service.
Here is a list of the supported properties:
- protocol - The protocol to use when accessing the remote service. (Required)
- iface - The interface that the remote service implements. (Required)
- host - The host where the remote service is located. (Defaults to 'localhost')
- port - The port on which the remote service is available. (Defaults to 1199 for RMI, 8080 for all other protocols)
- webcontext - The name of the webapp containing the remote service, as it appears in the webapp's URL. (A root webapp is assumed by default; ignored completely by RMI)
- url - The URL of the remote service. This overrides any host, port, and webcontext settings. (Optional)
When using the host, port, and webcontext properties, the plugin assumes that the remote service has been exported via a different instance of itself, and so constructs the service URL accordingly. In this case, the name of your service class must match the name of the exported service. See below a list of the standard URLs used.
4. That's it!
Once you have your server running, you can access your services via the configured remoting protocols. Just point your client(s) at the following URLs, where <host> is the hostname of the server, <app> is the name of you Grails app, and <service> is the class name of the exposed service, for example SecurityService.
| Protocol | URL |
|---|---|
| RMI | rmi://<host>:1199/<service> |
| Hessian | http://<host>:8080/<app>/hessian/<service> |
| Burlap | http://<host>:8080/<app>/burlap/<service> |
| HttpInvoker | http://<host>:8080/<app>/httpinvoker/<service> |

