|
|
|
Re: Starting scheduled job on server [message #1797783 is a reply to message #1796811] |
Tue, 06 November 2018 11:49 |
The Maddes 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 |
|
The Maddes wrote on Tue, 06 November 2018 06:49I 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 |
The Maddes 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 |
|
The Maddes wrote on Thu, 08 November 2018 06:22But 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.04473 seconds