ani03sha / redquark

Code that powers personal :nerd_face: website built using Gatsby, ReactJS :atom_symbol: and Node
https://redquark.org/
MIT License
11 stars 4 forks source link

aem/day-08-osgi-components-services/ #21

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Day 08 - OSGi Components and Services | Red Quark

Hello fellow developers 👋! In this post, we will dive into the basic blocks of OSGi bundles — Components and Services (remember OSGi Components are different from AEM authoring component). Software Modularity In modern times, complex software can be thought of as a collection of…

https://redquark.org/aem/day-08-osgi-components-services/

BalaramaRajuRudraraju commented 3 years ago

Hi Anirudh, I loved the article. It is simple and easy to understand at the same time it is detailed enough for in-depth understanding and it is elegant. Thank you very much. The Buy me coffee link doesn't work, request you to correct the link.

minhntp commented 3 years ago

Hi. Could you please explain these lines of code inside FetchTODO:

Runnable task = () -> {
            try {
                Thread.sleep(TODO_THREAD_SLEEP_TIME);
                while (!Thread.currentThread().isInterrupted()) {
                    // Get output from the API
                    String todoData = fetchData();
                    // Call the OSGi service which will write data into the repository
                    writeTODOService.writeData(todoData);
                    LOGGER.info("{}: Trying to write TODO data in repository at: {}", TAG, LocalDateTime.now());
                }
            } catch (InterruptedException e) {
                LOGGER.error("{}: Exception occurred: {}", TAG, e.getMessage());
            }
        };

I couldn't get it to work unless I change sleep time to a few seconds instead of 4 hours. And also the while loop keeps looping. I put a break after the last line in that while loop but it just keep looping. Thank you.

ani03sha commented 3 years ago

Hi @minhntp, answers to your query are inline -

Could you please explain these lines of code inside FetchTODO

This code is a java.lang.Runnable task which will query the API after the predefined interval. We are doing this with the help of "Multithreading" concept in Java.

I couldn't get it to work unless I change sleep time to a few seconds instead of 4 hours.

Yes, you did it right. For testing purposes, we can reduce the sleep time. This code is not meant for the production, it's just for the demo purpose.

the while loop keeps looping

The loop will terminate when the thread in which the current execution of program is present is interrupted (by whatever reason). Ideally, after Java 5, use of Executor framework is recommended instead of using Thread API directly. See this.