Responding to resource changes in the Eclipse workspace

原创 2004年08月10日 12:49:00

Many tools and user interface elements are interested in processing resource changes as they happen. For example, the task list wants to update new or changed markers, the navigator wants to reflect added and deleted resources, and the Java compiler wants to recompile modified Java files. Such notifications are potentially costly to compute, manage and broadcast. The Eclipse Platform resource model includes a series of mechanisms for efficiently notifying clients of resource changes. This article outlines these facilities and gives some examples of their use.

Resource change listeners

A good listener is not only popular everywhere,
but after a while, he knows something.

– Wilson Mizner

The primary method for Eclipse plug-ins to be notified of changes to resources is by installing a resource change listener. These listeners are given after-the-fact notification of what projects, folders and files changed during the last resource changing operation. This provides a powerful mechanism for plug-ins to keep their domain state synchronized with the state of the underlying workspace. Since listeners are told exactly what resources changed (and how they changed), they can update their model incrementally, which ensures that the time taken by the update is proportional to the size of the change, not the size of the workspace.

Listeners must implement the IResourceChangeListener interface, and are registered using the method addResourceChangeListener on IWorkspace. It is also important to remove your resource change listener when it is no longer needed, using IWorkspace.removeResourceChangeListener.

During a resource change notification, the workspace is locked to prevent further modification while the notifications are happening. This is necessary to ensure that all listeners are notified of all workspace changes. Otherwise, a change made by one listener would have to be broadcast to all other listeners, easily creating the possibility of an infinite loop. There is a special exception to this rule for the PRE_AUTO_BUILD and POST_AUTO_BUILD event types that will be discussed later on.

Before we get into the details, let's start with a simple example that shows how to add and remove a resource change listener:

   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   IResourceChangeListener listener = new IResourceChangeListener() {
      public void resourceChanged(IResourceChangeEvent event) {
         System.out.println("Something changed!");

   //... some time later one ...

Below is an example of an operation that is nested using the IWorkspaceRunnable mechanism. In this case, a single resource change event will be broadcast, indicating that one project and ten files have been created. To keep it simple, progress monitoring and exception handling have been omitted from this example.

   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   final IProject project = workspace.getRoot().getProject("My Project");
   IWorkspaceRunnable operation = new IWorkspaceRunnable() {
      public void run(IProgressMonitor monitor) throws CoreException {
         int fileCount = 10;
         for (int i = 0; i < fileCount; i++) {
            IFile file = project.getFile("File" + i);
            file.create(null, IResource.NONE, null);
   };, null);
details please visit

eclipse: workspace出错导致无法启用的解决

通常我们会在eclipse中创建多个workspace,比如一个用于学习,一个用于工作... ,因为种种原因,时不时会发现eclipse切换workspace后启动失败,提示让你去看workspace...
  • wjyyxzzjnws
  • wjyyxzzjnws
  • 2015年04月15日 10:09
  • 2485

Eclipse用法与技巧——导入工程时报错(already exist in the workspace)

当我们导入同一个工程多次时,从第二次开始,导入时eclipse会提示“Some projects cannot be imported because they already exist in th...
  • yuan882696yan
  • yuan882696yan
  • 2014年05月27日 10:58
  • 1506

MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to r

!SESSION 2015-01-19 20:59:03.289 ----------------------------------------------- eclipse.buildId=I2...
  • yuexuning58
  • yuexuning58
  • 2015年01月19日 21:11
  • 1703

The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes

eclipse.buildId=I20100608-0911 java.version=1.6.0_18 java.vendor=Sun Microsystems Inc. BootLoader...
  • shenhonglei1234
  • shenhonglei1234
  • 2011年03月11日 11:48
  • 5595

Eclipse is not responding

一 前言 今天处理一个超大项目 文件,光代码文件就有4000多个,以至于运行后CPU0満负荷运行。在切换到其它程序工作后返回到eclipse发现eclipse只剩下一个窗口框架,里面一片空白,没有任...
  • duguduchong
  • duguduchong
  • 2013年04月19日 10:54
  • 2913

eclipse老是卡在Refreshing workspace

最近老是遇到eclipse没有响应,参考网络上的相关帖子做了些优化,但问题依旧。 后来发现出现这个问题一般都是eclipse无响应,直接结束进程后导致的,看了下eclipse日志,果真如此。 ...
  • liuzhujun
  • liuzhujun
  • 2013年10月10日 16:09
  • 6112


上一篇分析appium的文章还要追述到一个月前了,感慨已经荒废了很久了。今天继续开始吧。 控制器模块// Appium webserver controller methods // https://...
  • qhshiniba
  • qhshiniba
  • 2015年04月29日 12:26
  • 2455

修改mapper文件报错 Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘ac...
  • ppwwp
  • ppwwp
  • 2018年02月12日 11:20
  • 43


现象: 现在大多数java开发者使用的IDE都是eclipse,而且一般在一个workspace中,会有很多个项目,有些是open的,有些是关闭的。有时候eclipse在使用过程中会莫名其妙的异...
  • littlegirlya
  • littlegirlya
  • 2014年12月12日 11:19
  • 2601

eclipse启动无响应,老是加载不了revert resources,或停留在Loading workbench状态

做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了。启动画面始终停留在Loading workbench状态。反复重启,状态依旧。        多数情况...
  • lq18111292117
  • lq18111292117
  • 2016年05月26日 10:08
  • 7571
您举报文章:Responding to resource changes in the Eclipse workspace