Expand my Community achievements bar.

SOLVED

OSGI component is in satisfied state but not in active while we include Java Watch service in it

Avatar

Level 2

Hi All,

I have a osgi component  and i have added java watch service (as a listener to watch directories) in it. So once bundle is installed, component goes to satisfied state.(but main bundle will be in active state)

Because of this, other components will be disabled. I am forced to enable them manually.

If I remove watch service from osgi component, it works fine. Please let me know if i am missing anything.

Code:

@Activate

@Modified

protected void activate(final Map<String, Object> properties) {

WatchService watchService = null;

WatchKey key = null;

try {

     watchService = FileSystems.getDefault().newWatchService();

     if ( StringUtils.isNotEmpty("somePath")) {

          Path path = Paths.get("somePath");

          path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);

          while ((key = watchService.take()) != null) {

                     for (WatchEvent<?> event : key.pollEvents()) {

                             // do some action

                         }

            key.reset();

                    }

          } else {

               watchService.close();

          }

} catch (IOException | InterruptedException e) {

          logger.error("Exception {}", e);

     }

}

second issue is, deactivate is not working.

code:

@Deactivate

protected final void deactivate(final Map<String, String> properties) {

logger.info("inside deactivate");

//disable watch event

}

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

as PuzanovsP​ pointed out, you are likely to block the thread, which is reponsible to startup the system (this would also explain the described behavior). Do not block threads in the activate methods, they should be fast. If you need to have a thread to run the WatchService, either use a Sling Threadpool for it or create and manage your own thread.

Jörg

View solution in original post

8 Replies

Avatar

Level 10

Could you share requirements, complete file & other relevant artifacts?

I'm not sure about usage of FileSystems in OSGI. You may explore BundleContext or sling filesystem provider for this use case, if that helps

BundleContext context = FrameworkUtil.getBundle( this.getClass() ).getBundleContext();

Avatar

Employee Advisor

I've seen the case that a bundle is resolved, but not getting active only in cases if you have a bundle activator which throws an exception.

Please check the logfiles for any messages regarding your bundle or the services within.

Jörg

Avatar

Level 10

When you open the OSGi bundle view - are you seeing any red packages?

Avatar

Level 2

Hi,

Thank for the response. I have to keep watching a shared folder. If there is any new document added to it, i will be processing it and uploading to aem. Java WatchService acts as a listener, So I activate watchservice from osgi component activate method.

We can get instance of watchservice only from file systems i feel. I need to figure it out if there are other ways.

Bundle becomes satisfied when i include watchservice else it will be in active state.

Avatar

Level 2

Thank you for the response. This blog is helpful, but my requirement is quite different.

User added new document to watcher folder, and he will wait till processing of document is completed and later he will be notified with response.

I am trying to use Java Watch service because, i want event to get triggered on adding any new document to folder.if i use schedular, i will have to keep polling for every interval, so i cant send notification to user while he adds document to watched folder( as i will not know his specific file)

Avatar

Level 2

Thank you Jorg for the response.

I see no error as such. bundle works fine. functionality is working even in satisfied state. Only issue is, its making other osgi comppnents disabled on installing bundle. I will have to enable them manually. 

If my service is active instead of statisfied, i see all components enabled by default on installing osgi bundle. So i am trying to figure iout how java watch service is making this difference.

Avatar

Community Advisor

Hey SDK,

First test your code in a JUnit test without any OSGi, if it works, then create OSGi service and put your code into a Thread, with an OSGi service to call when something is read.

Launch your listener in a separate Thread, do not block OSGi system with your watcher or custom logic you want to execute.

Pass reference to OSGi service that should do some action, this way a) Activate b) Deactivate will work and OSGi system won't be affected.

Regards,

Peter

Avatar

Correct answer by
Employee Advisor

Hi,

as PuzanovsP​ pointed out, you are likely to block the thread, which is reponsible to startup the system (this would also explain the described behavior). Do not block threads in the activate methods, they should be fast. If you need to have a thread to run the WatchService, either use a Sling Threadpool for it or create and manage your own thread.

Jörg