0

我需要在我的 grails 应用程序中创建一个石英作业,它应该调用另一个应用程序的 servlet。在 servlet doGet() 方法中,我需要接收传递的消息并执行该过程,一旦结束,需要将响应发送回服务。我是新手,任何人都可以帮助我。将在 grails 应用程序中创建一个作业并从该方法调用一个服务方法我如何调用另一个应用程序的 servlet doGet()。

这是我的石英工作

class DBCleanUpJob {

    def concurrent = false
        def miscBillService

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

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

这是我的服务

 @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();
         }
     }
 }

上面的代码是否调用了servletdoGet()方法?

4

1 回答 1

0

我使用其余客户端插件,https://github.com/grails-plugins/grails-rest-client-builder/

将以下内容添加到 build.gradle

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

然后为您服务...

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 )

上面假设您正在 POST 回 xml,如果没有,这当然需要更改。

于 2017-02-14T10:08:24.593 回答