0

I need to create a quartz job in my grails application which should call a servlet of another application. And in the servlet doGet() method i need to receive the message that is passed and do the process and once it is over need to send a response back to the service. I am new to this can anyone help me out. will create a job in the grails application and call a service method from this method how do I call the servlet doGet() of another application.

This is my quartz job

class DBCleanUpJob {

    def concurrent = false
        def miscBillService

        static triggers = {
        cron name : 'myTrigger', cronExpression : "0 0 2 * 1 ?"
    }

        def execute() {
        miscBillService.miscBillCall()
    }
}

And this is my service

 @Transactional
     class MiscBillService {


     def miscBillCall() {

         String line;
         try
         {
             URL url = new URL("http://127.0.0.1/MServlet?value=run start");
             BufferedReader ins = new BufferedReader(new InputStreamReader(url.openStream()));
             line = ins.readLine();

             System.out.println(line);

             ins.close();
         }
         catch (Exception e)
         {
             e.printStackTrace();
         }
     }
 }

Does the above code call the servlet doGet() method?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

1 Answers1

0

I use the rest client plugin, https://github.com/grails-plugins/grails-rest-client-builder/

Add the following to build.gradle

dependencies {
    compile "org.grails:grails-datastore-rest-client:5.0.0.RC2"
    ...
}

Then in your service...

import grails.plugins.rest.client.RestBuilder

def yourGetUrl = 'http://getUrl'
def rest = new RestBuilder()
def resp = rest.get( yourGetUrl )
// do some validation on response & process
def xml = processGetResponse( resp )
def yourPostUrl = 'http://postUrl'
resp = rest.post( yourPostUrl ) {
    contentType "text/xml; charset=ISO-8859-1"
    xml xmlData
}
processPostResponse( resp )

Above assumes you're POSTing xml back, this will of course need changing if not.

Mike W
  • 3,853
  • 2
  • 11
  • 18
  • i just need to call the doGet() method in the servlet passing "start" as a parameter and the servlet does some automatic processing and once it is done it should returna response string "complete" to the service. –  Feb 14 '17 at 10:21
  • Have you written a `processGetResponse( resp )` and `processPostResponse( resp )` method? The code above is 90% complete but I don't know what the response is from your endpoint so you have to do a little bit of work, read the docs in the link sent and play around with the resp object. – Mike W Feb 14 '17 at 10:25
  • the compile "org.grails:grails-datastore-rest-client:5.0.0.RC2" will not work as my proxy setting will not allow to download the dependencies –  Feb 14 '17 at 10:30
  • so how to i get the response string in the service method –  Feb 14 '17 at 10:34
  • You should get a RestResponse so try resp.text or resp.xml or resp.json – Mike W Feb 14 '17 at 10:41
  • as i cannot use the code that you have posted as my proxy will not allow to download the plugins –  Feb 14 '17 at 10:43
  • According to https://grails.org/plugin/rest-client-builder for grails 3 it's not actually a plugin now, we're just specifying a dependency on a core grails data library, did you try adding the line to the dependencies block in build.gradle? – Mike W Feb 14 '17 at 10:53
  • i added the compile "org.grails:grails-datastore-rest-client:5.0.0.RC2" in BuildConfig as i dont have build.gradle –  Feb 14 '17 at 10:58
  • i am using grails 2.5.3 –  Feb 14 '17 at 10:58