Remotely call action in grails and grails call action
When performing a program similar to batch processing, if you need to save a large number of records in an action, this will cause the database session in grails to exceed the load, resulting in OOM.
This is because all the modifications made to the data in a request are saved in the database session of the request. The database session will be released only after the request ends, in this way, if the number of data records modified in this request is too large, OOM will occur.
We use the mongodb database and grails mongodb plugin. Even if we use the underlying link of mongodb In This plugin for operations, OOM still occurs.
Therefore, we want to make each request retain only a small amount of modification data in the database session, which is equivalent to writing the modification of specific data in another action. The sample code is as follows:
def updateData(){ println "classId:${params.classId} usersId:${params.usersId}" render "ok" }
The method to call this action is:
println dataService.remoteCall(request, g.createLink(action: 'updateData', params: [classId:"test1", usersId:'usersIdxxx']))
The key to implementation here is the function implementation of dataService. remoteCall, which is as follows:
/*** Get the protocol and Host IP url without/* For example: * http: // localhost: 9090/blog/bkoff/testCaculateUserExedCount * returned: http: /localhost: 9090 * @ param request * @ return */private String getHostUrl (request) {String requestUrl = request. requestURL String seperator = ": //" int index = requestUrl. indexOf (": //") index = requestUrl. indexOf ("/", index + seperator. length () return requestUrl. substring (0, index)}/*** call a method remotely, you can create * @ param request * @ param actionUrl * @ return */def String remoteCall (request, actionUrl) {def hostUrl = getHostUrl (request) in createLink mode) def url = new URL (hostUrl + actionUrl) return url. text}