Mar 01 2009

Defining a background Job to run periodically

Published by under Eclipse,IT,Java

One may need to run some task periodically in the background in Eclipse. This background task should run automatically and shouldn’t block user from regular usage of the application. My proposal for accomplishing this is using Eclipse Jobs API and org.eclipse.ui.startup extension-point.

  1. Adding org.eclipse.ui.startup extension:
    • Dependencies:
      • org.eclipse.ui
      • org.eclipse.core.runtime
    • Extension:
      <?xml version="1.0" encoding="UTF-8"?>
      <?eclipse version="3.2"?>
      <plugin>
         <extension point="org.eclipse.ui.startup">
            <startup class="tk.urbas.eclipse.urbanlife.Startup"></startup>
         </extension>
      </plugin>
  2. Implementing Job:
    package tk.urbas.eclipse.urbanlife;
     
    import org.eclipse.core.runtime.IProgressMonitor;
    import org.eclipse.core.runtime.IStatus;
    import org.eclipse.core.runtime.Status;
    import org.eclipse.core.runtime.jobs.Job;
     
    public class RefreshDataJob extends Job {
     
        public RefreshDataJob(String name) {
            super(name);
        }
     
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            // Work to do in the background
            return Status.OK_STATUS;
        }
    }
  3. Scheduling Job to run periodically:
    package tk.urbas.eclipse.urbanlife;
     
    import org.eclipse.core.runtime.jobs.IJobChangeEvent;
    import org.eclipse.core.runtime.jobs.Job;
    import org.eclipse.core.runtime.jobs.JobChangeAdapter;
    import org.eclipse.ui.IStartup;
     
    public class Startup implements IStartup {
     
        private static final long STARTUP_DELAY = 5000; // 5 seconds delay for first run
        protected static final long JOB_INTERVAL = 60000; // Job should run every 60 seconds
     
        public void earlyStartup() {
            final Job updateJob = new RefreshDataJob("Refreshing data in the background");
            updateJob.schedule(STARTUP_DELAY);
            updateJob.addJobChangeListener(new JobChangeAdapter() {
                @Override
                public void done(IJobChangeEvent event) {
                    super.done(event);
                    updateJob.schedule(JOB_INTERVAL);
                }
            });
        }
    }

Let me know if you have any better solution for implementing this. All improvements, suggestions and comments will be greatly appreciated.

10 responses so far

Feb 20 2009

Eclipse plug-in as a web service client

Published by under Eclipse,IT,Java

Recently I was playing around consuming web service inside Eclipse plug-in. Making simple web service call maybe a bit tricky at the beginning. I’m going to provide quick start instruction here,

Required software:

  • Eclipse for RCP/Plug-in Developer
  • JEE Standard Tools & Axis
    JEE Standard Tools

Configuration steps:

  • Define new JEE service in a workspace
    Defining JEE server
  • Create new plug-in project
    Creating new plug-in project
  • Add following dependencies to the plug-in:
    • org.apache.commons.logging
    • javax.wsdl
    • javax.xml.rpc
    • javax.xml.soap
    • org.apache.commons.discovery
    • org.apache.axis

    Web service client dependencies

  • Generate web service client code
    Generating web service client code

    Generating web service client code based on WSDL

Consume web service:

Using web service in side a plug-in is no different than anyone else. Sample of invoking web service method:

TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();

Instruction presented above is based on Eclipse 3.4.1 and uses sample web service from http://www.urbanlife.pl/

8 responses so far

Feb 11 2009

Automatic PDE build with Pluginbuilder

Published by under Eclipse

Pluginbuilder is Eclipse tool that generates build configuration based on PDE build. By default the tool generates map file containing the information how source code of the plug-ins should be obtained. Supported methods are: CVS, GET and COPY. Assuming that you want to build your features or product from source files that are checked-out from repository independently from the build process COPY options seems to look reasonable. There are few problems with that:

  • it requires absolute paths to plug-ins/features locations
  • every single plug-in has to be listed there

Workaround for this is not using map file at all and provide appropriate build directory structure outside. To skip fetching resources based on map file configuration one need to set skipMaps property in build.properties file. Next step is to configure getting plug-ins/features resources into build directory. Recommended phase for copying the source code is preSetup phase in PDE build. To hook into this phase standard ant task for copying the files/directories in to build directory have to be invoked in preSetup target in customTargets.xml (it is generated by Pluginbuilder as well as other configuratoin files in build-files directory).

Those two modifications allow to make use of Pluginbuilder generated scripts to build Eclipse based applications without coupling it tightly to version control system.

One response so far

Feb 08 2009

WordPress upgrade

Published by under IT

I have finally upgraded my WordPress installation to version 2.7 … from version 2.3.2 – shame on me.

Upgrade went smoothly, my first impression: automatic upgrade of the plug-ins is really great!

No responses yet

Feb 08 2009

7 things you may not know about me

Published by under General

Some time ago Jakub suggested that I write 7 things most people may not know about me.

  1. I read political books. Waldemar Łysiak is one of my favorite authors.
  2. I watch many TV series, probably too many while I mix the plots some times.
  3. I hate ‘nothing-to-do’ state. It’s probably because I was pretty busy in last 2 years of my master studies (sharing time between full-time studying at AGH University and part-time working at Sabre Holdings). I just got used to the very busy schedules.
  4. I’m a big fan of beer. Wherever I go I try to get a beer from local breweries (the smaller the better). I try to avoid drinking global brands. It’s not that fun to drink the same (or almost the same) beer everywhere around the globe.
  5. In was doing pretty well in high school. I received a scholarship from Polish Prime Minister and I was the best graduate-student (2003).
  6. I haven’t been playing video games since I was 15. They are too complicated for me nowadays ;-(
  7. I was doing some .NET development in university times. Together with a team we were participating in Microsoft Imagine Cup (national finals in Software Design category 2006 and 2007). Unfortunately since I graduated I don’t do it any more. It’s hard to follow Java and .NET worlds simultaneously ;-(

Traditionally I encourage a few folks to share their lists as well:

2 responses so far

Feb 04 2009

Eclipse Jobs API – Errata

Published by under Eclipse,IT,Java

My colleague Lukasz Milewski has just spotted a missing piece in a code sample for creating icons presented in previous post. In the screenshot you can see nice icons for currently running Jobs as well as for scheduled Jobs. You need to set two properties in your Job implementation to associate an icon with it.

	setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
	setProperty(IProgressConstants.ICON_PROPERTY, ImageDescriptorFactory
		.createImageDescriptor(ICON_NAME));

4 responses so far

Jan 14 2009

Using Eclipse Jobs API

Published by under Eclipse,IT,Java

Eclipse platform offers great API for managing long running operations in the background. Besides the API for starting, stopping and monitoring progress of the Job – there is also standard UI for managing all those operations. I’m going to present sample usage of Jobs API in you own plug-in/application.

Creating and scheduling Job may look like this:

 TrainJob job = new TrainJob(TRAIN_JOB_NAME + file.getName(), classifier);
 job.setRule(file);
 job.setUser(true);
 job.setPriority(Job.SHORT);
 job.schedule(); // start as soon as possible

Sample of custom Job:

package pl.edu.agh.caracal.classifier.popup.jobs;
 
import static org.eclipse.core.runtime.Status.OK_STATUS;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import pl.edu.agh.caracal.classifier.ext.classifiers.AbstractClassifier;
 
/**
 * Class for execution of training of a classifier
 *
 * @since 1.0
 */
public class TrainJob extends Job {
 private AbstractClassifier classifier;
 
/**
  * Public constructor TrainJob
  *
  * @param name Train job name
  * @param classifier Classifier to be trained
  */
 public TrainJob(String name, AbstractClassifier classifier) {
 	super(name);
 	this.classifier = classifier;
 }
 
 @Override
 protected IStatus run(IProgressMonitor monitor) {
 	// Long running operation - in this case classifier training
       // ...
       return OK_STATUS;
 }
 
}

Standard GUI for presenting Job progress:

Jobs API View

3 responses so far

Sep 09 2008

Eclipse RCP tips and tricks

Published by under Eclipse,IT,Java

Recently I had to do couple of tricks Eclipse RCP applications. Basically all of the pieces of code presented below need to be placed in method initialize(IWorkbenchConfigurer configurer). You need to override this method in WorkbenchAdvisor subclass.

  • Displaying icons in Navigator View
    final String ICONS_PATH = "icons/full/";
    final String PATH_OBJECT = ICONS_PATH + "obj16/";
    Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH);
    declareWorkbenchImage(configurer, ideBundle,
    IDE.SharedImages.IMG_OBJ_PROJECT, PATH_OBJECT + "prj_obj.gif", true);
    declareWorkbenchImage(configurer, ideBundle,
    IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, PATH_OBJECT
    + "cprj_obj.gif", true);
  • Enabling Decoration Context
    DecorationContext dc = (DecorationContext) DecorationContext.DEFAULT_CONTEXT;
    dc.putProperty(IDecoration.ENABLE_REPLACE, Boolean.TRUE);
  • Enabling Jobs Progress Bar
    IWorkbench wb = PlatformUI.getWorkbench();
    wb.getProgressService();

Probably you don’t need any of those when running your plug-ins in Eclipse IDE. The tricky part is running it in a stripped to minimum RCP based applications.

One response so far

Sep 05 2008

Firefox, Chrome – WTF?

Published by under IT

Chrome installed, Firefox set as default browser.

Warning during opening GMail from GTalk:

Chrome - WTF?

WTF?

3 responses so far

Sep 02 2008

Google Chrome

Published by under IT

I have just installed a newest baby of Google on my laptop… Google Chrome – a web browser. It’s fast… and fully integrated with Google services… and stable (per Google). I’m afraid of this monster… 

No responses yet

« Prev - Next »