Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Starting scheduled job on server
Starting scheduled job on server [message #1796811] Fri, 19 October 2018 09:36 Go to next message
Miloslav Frajdl is currently offline Miloslav FrajdlFriend
Messages: 47
Registered: June 2018
Member
Hi.
I'm beginner in Scout framework and I have one trouble.

I have Scout web application. I need to create a scheduled job that will run on the server. Here I found solution:
https://www.eclipse.org/forums/index.php/t/234236/

But I do not know where is the ServerApplication.Start method. Where do I start a job? I can not find it.

Thank you very much for the advice.

Re: Starting scheduled job on server [message #1796812 is a reply to message #1796811] Fri, 19 October 2018 09:51 Go to previous messageGo to next message
Matthias OtterbachFriend
Messages: 55
Registered: August 2015
Location: Munich
Member
The mentioned thread/reply uses an older version of Scout.

With the current version, you could probably just create a org.eclipse.scout.rt.platform.IPlatformListener which reacts on a org.eclipse.scout.rt.platform.IPlatform.State.PlatformStarted event. An example for such a IPlatformListener would be available at https://github.com/BSI-Business-Systems-Integration-AG/org.eclipse.scout.docs/blob/releases/8.0.x/code/bahbah/org.eclipsescout.demo.bahbah.server/src/main/java/org/eclipsescout/demo/bahbah/server/ServerApplication.java which coincidentally has been named ServerApplication. Of course you could also add code to an existing IPlatformListener.

If it is a one time job which should be run at a specified time with a current version of Scout the code might also be a lot easier, you could try something similar to:

      Jobs.schedule(() -> System.out.println("One minute remaining"), Jobs.newInput()
          .withExecutionTrigger(Jobs.newExecutionTrigger()
              .withStartAt(DateUtility.parse("20181231 235900.000", "yyyyMMdd HHmmss.SSS"))));

[Updated on: Fri, 19 October 2018 09:55]

Report message to a moderator

Re: Starting scheduled job on server [message #1796826 is a reply to message #1796812] Fri, 19 October 2018 13:09 Go to previous messageGo to next message
Miloslav Frajdl is currently offline Miloslav FrajdlFriend
Messages: 47
Registered: June 2018
Member
Thank you for your answer, it works.
Re: Starting scheduled job on server [message #1797783 is a reply to message #1796811] Tue, 06 November 2018 11:49 Go to previous messageGo to next message
The Maddes is currently offline The MaddesFriend
Messages: 44
Registered: October 2018
Member
Hey, since this is the closest what matches my question, i think i will ask here before making up a new thread.

I am thinking of something similar and wanted to ask which is the way to go.

Siince you said its an outdated example, i wanted to know how to handle the following task.

I need a job that will run once a month, visit an url and download/read a xml file, take the parts needed and saves it to the database.

So first i need to run that on startup of the application, and then every month on a specific date.

Is the visiting of the url etc possible inside scout out of the box? If so, could you help me out with some example how to handle it?

I read the documentation of technical guide of jobs, but most part is about schaduling and i dont see a lot of functionality (at least not for me due to being new to the java and scout world)

So im sorry if that question is already answered somehow and appriciate your effort.
Thank you very much in advance!

[Updated on: Tue, 06 November 2018 11:59]

Report message to a moderator

Re: Starting scheduled job on server [message #1797877 is a reply to message #1797783] Wed, 07 November 2018 21:40 Go to previous messageGo to next message
Beat Schwarzentrub is currently offline Beat SchwarzentrubFriend
Messages: 202
Registered: November 2010
Senior Member
The Maddes wrote on Tue, 06 November 2018 06:49
I need a job that will run once a month, visit an url and download/read a xml file, take the parts needed and saves it to the database.


You can use any technique or library you want to download a file. Probably the simplest way is to create a java.net.URL and read its stream. Scout's IOUtility might come in handy.

Quote:
So first i need to run that on startup of the application, and then every month on a specific date.


Use a platform listener. This gets executed automatically when the application starts. You can then schedule your job to be executed automatically (scheduling it prevents the application startup to be blocked by the job). Then, schedule the same job again using a recurring pattern. Scout uses the Quartz framework to schedule recurring tasks. Various schedule builders are already included, just open "org.quartz.ScheduleBuilder" in Eclipse and hit F4 to see all subclasses.

Here is a very simplified example:

import java.net.URL;

import org.eclipse.scout.rt.platform.IPlatform.State;
import org.eclipse.scout.rt.platform.IPlatformListener;
import org.eclipse.scout.rt.platform.PlatformEvent;
import org.eclipse.scout.rt.platform.job.Jobs;
import org.eclipse.scout.rt.platform.util.IOUtility;
import org.eclipse.scout.rt.platform.util.concurrent.IRunnable;
import org.quartz.CronScheduleBuilder;

public class Startup implements IPlatformListener {

  @Override
  public void stateChanged(PlatformEvent event) {
    if (event.getState() == State.PlatformStarted) {
      IRunnable job = () -> {
        // Your code here!
        String content = IOUtility.readStringUTF8(new URL("http://ip-api.com/xml").openStream());
        System.out.println("Your IP: " + content);
      };

      // Run now (asynchronous, so application will not be blocked)
      Jobs.schedule(job, Jobs.newInput());
      // Schedule using a custom execution trigger with a cron schedule
      Jobs.schedule(job, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger()
          .withSchedule(CronScheduleBuilder.monthlyOnDayAndHourAndMinute(25, 0, 0)))); // execute at 00:00 on each 25th
    }
  }
}


Regards,
Beat
Re: Starting scheduled job on server [message #1797904 is a reply to message #1797877] Thu, 08 November 2018 11:22 Go to previous messageGo to next message
The Maddes is currently offline The MaddesFriend
Messages: 44
Registered: October 2018
Member
Hey Beat,
thank you a lot for the hints, i managed to read and parse everything to my needs so far.
But one problem still remains, saving it to the database.
In my Service i created a method
public void insertParsedDataToDatabase(Type value1, Type value2, ...) {
SQL.insert(SQLS.INSERTSTRING, new NVPair("value1", value1),
...);


And in my Job i call the service like
IInsertService service = BEANS.get(IInsertService.class);
service.insertParsedDataToDatabase(value1, value2,....)


But then i get an AssertException Assertion error : Transaction required.
org.eclipse.scout.rt.platform.util.Assertions$AssertionException: Assertion error: Transaction required
	at org.eclipse.scout.rt.platform.util.Assertions.fail(Assertions.java:629)
	at org.eclipse.scout.rt.platform.util.Assertions.assertNotNull(Assertions.java:87)
	at org.eclipse.scout.rt.server.jdbc.AbstractSqlService.getTransaction(AbstractSqlService.java:578)
	at org.eclipse.scout.rt.server.jdbc.AbstractSqlService.insert(AbstractSqlService.java:641)
	at org.eclipse.scout.rt.server.jdbc.SQL.insert(SQL.java:128)

Re: Starting scheduled job on server [message #1797908 is a reply to message #1797904] Thu, 08 November 2018 11:54 Go to previous messageGo to next message
Beat Schwarzentrub is currently offline Beat SchwarzentrubFriend
Messages: 202
Registered: November 2010
Senior Member
The Maddes wrote on Thu, 08 November 2018 06:22
But then i get an AssertException Assertion error : Transaction required.


Your code is running outside a "RunContext". The run context provides information about the transaction, but without run context you also don't have a transaction.

To fix this, simply create a new run context and pass it to the job manager when scheduling the job:
Jobs.schedule(..., Jobs.newInput()
  .withRunContext(RunContexts.empty())
  ...
);


This is not required in normal exec... actions from menus, buttons etc. because then, Scout already provides the correct run context for you. Only when running code from "outside" you have to span the context by yourself.

Beat
Re: Starting scheduled job on server [message #1797913 is a reply to message #1797908] Thu, 08 November 2018 12:52 Go to previous message
The Maddes is currently offline The MaddesFriend
Messages: 44
Registered: October 2018
Member
Thank you very much! Thats what i was missing. Now i see how to deal with that kind of stuff, your explanation was perfect!
Thanks again for your patience and effort!
Previous Topic:AbstractMobileStandaloneRwtEnvironment cannot be resolved to a type
Next Topic:Error on the start of the simple project
Goto Forum:
  


Current Time: Fri Mar 29 00:47:29 GMT 2024

Powered by FUDForum. Page generated in 0.02337 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top