Implement remote rpc in grails

Source: Internet
Author: User
Tags grails
> Remote rpc is the most convenient option for cross-platform/domain remote calls in grails.
>
> Grails remoting plug-in http://grails.org/plugin/remoting is the best choice, it supports RMI, Hessian, burlap, httpinvoker, xfire protocol.

Install
---

Grails install-plugin remoting

Server implementation
---
We plan to use the Hessian protocol as an example.

1. Define the RPC Interface
Write the RPC interface in the 'src/Java' directory of the grails project. For example, set an interface.

Java:

Public interface isynchronizeservice {
/**
* Obtain a category
* @ Param Client Version
* @ Return
*/
Series getseries (long client_version );

/**
* Obtain elements
* @ Param Client Version
* @ Return
*/
Item getitems (long client_version );
}

This interface provides two methods to obtain different data.
Then, create the pojo class in the 'src/Java' directory.

Series. Java

Java:

/**
* Pojo Classification
*/
Public class series implements serializable {

Private Static final long serialversionuid =-1666133446403499930l;

/**
* Category name
*/
Private string name;

/**
* @ Return the name
*/
Public String getname (){
Return name;
}

/**
* @ Param name the name to set
*/
Public void setname (string name ){
This. Name = Name;
}

Public series (){}
}

Item. Java skipped, same as series. Java

These pojo that require remote transmission must be serializable, so the involved pojo must implement the serializable interface.

2. Implement RPC services
Create a new grails service with the following names:

Java:

Class synchronizeservice implements isynchronizeservice {
// Expose the WebService as Hessian Protocol
Static expose = ['hessian ']

/**
* Synchronous Classification
* @ Param Client Version
* @ Return
*/
@ Override
Public series getseries (long client_version ){
// Query from database
Def sseries = storeseries. findbyid (1)
// Assemble pojo
Def series = new series ()
Series. setname (sseries. Name)
Return series;
}

/**
* Synchronization Element
* @ Param Client Version
* @ Return
*/
@ Override
Public item getitems (long client_version ){
// Query from database
Def sitem = storeitem. findbyid (1)
// Assemble pojo
Def item = new item ()
Item. setname (sitem. Name)
Return item;
}

}

The static attribute expose is used to specify the RPC protocol exposed by the Service. Here we specify Hessian. Of course, multiple protocols can also be specified at the same time. Expose is a list.

Client implementation
---
Clients that support the hessiani protocol can remotely call our above grails service.
Here we still use grails to call.

1. Import Interface
You can copy all RPC-related Java classes under the 'src/Java' directory on the server side, or generate jar packages for these Java classes on the server side, and then import them. In short, these interfaces must ensure that the server and the client are the same (the class name and method are the same ).

2. Set remote RPC service
Create a new grails service. The service name must be consistent with the grails service name of the server:

Java:

Class synchronizeservice {
/**
* Remote Call Parameters
*/
Static remote = [
Protocol: 'hessian ', // RPC protocol
Iface: isynchronizeservice, // Interface Class Name

// The URL of the server is http: // localhost: 8081/rpcserver
HOST: 'localhost', // remote address
Port: '000000', // remote port
Webcontext: 'rpcserver' // The context name of the webapp, that is, the name in the URL
]

}

This service only needs to include these items. The important thing is to set the 'protocol' and 'host' and 'webcontext' values. 'Ifase' is the class name of the interface. To include the registration, such as Foo. Bar. isynchronizeservice, the Class Loader must be able to find it. This service will automatically inject interface-related code when grails is started.

3. access remote services
Create a controller and write an action:

Atcion

Java:

Def testrpc (){

Def series = synchronizeservice. syncseries (1l)

Render series. getname ()
}

Note that no, the interface method will be automatically injected into the service, and the plug-in will implement the corresponding code in the interface according to the protocol. Easy to handle.

Of course, the client can be any language or platform that supports the remoting protocol. Such as Android and IOS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.