Jakarta CDI - Intermediate Lab 3
In this lab, we will explore more about the CDI events.
1. Create the LoggerProducer
class
Steps
- Create a new package called
news
in theexpert.os.labs.persistence.cid
package - Create the class
News
in theexpert.os.labs.persistence.cid.news
- Implements the
Supplier<String>
interface and it's methodget()
- Add a private
String
field namedname
- Create a constructor to associate its name
- Add a factory method
of()
that will create a new instance ofNews
- Make the
get()
method return the value of thename
attribute
Solution
Click to see...
2. Create the MAgazine
class
Steps
- Create a class called
Magazine
in theexpert.os.labs.persistence.cid.news
package - Implements the
Consumer<News>
and the methodaccept()
- the
Consumer
class is from thejava.util.function
package
- the
- Define a private attribute
Logger
from thejava.util.logging
package - Add the
@Observes
annotation to theNews
attribute in theaccept()
method, where it's from thejakarta.enterprise.event
package - Log the
News
in theaccept()
method
Solution
Click to see...
import jakarta.enterprise.event.Observes;
import java.util.function.Consumer;
import java.util.logging.Logger;
public class Magazine implements Consumer<News> {
private static final Logger LOGGER = Logger.getLogger(Magazine.class.getName());
@Override
public void accept(@Observes News news) {
LOGGER.info("We got the news, we'll publish it on a magazine: " + news.get());
}
}
3. Create the NewsPaper
class
Steps
- Create a class called
NewsPapper
in theexpert.os.labs.persistence.cid.news
package - Annotate it with
@ApplicationScoped
from thejakarta.enterprise.context
package - Implements the
Consumer<News>
and the methodaccept()
- the
Consumer
class is from thejava.util.function
package
- the
- Define a private attribute
Logger
from thejava.util.logging
package - Add the
@Observes
annotation to theNews
attribute in theaccept()
method, where it's from thejakarta.enterprise.event
package - Log the
News
in theaccept()
method
Solution
Click to see...
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import java.util.function.Consumer;
import java.util.logging.Logger;
@ApplicationScoped
public class NewsPaper implements Consumer<News> {
private static final Logger LOGGER = Logger.getLogger(NewsPaper.class.getName());
@Override
public void accept(@Observes News news) {
LOGGER.info("We got the news, we'll publish it on a newspaper: " + news.get());
}
}
4. Create the SocialMedia
class
Steps
- Create a class called
SocialMedia
in theexpert.os.labs.persistence.cid.news
package - Annotate it with
@ApplicationScoped
from thejakarta.enterprise.context
package - Implements the
Consumer<News>
and the methodaccept()
- the
Consumer
class is from thejava.util.function
package
- the
- Define a private attribute
Logger
from thejava.util.logging
package - Add the
@Observes
annotation to theNews
attribute in theaccept()
method, where it's from thejakarta.enterprise.event
package - Log the
News
in theaccept()
method
Solution
Click to see...
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import java.util.function.Consumer;
import java.util.logging.Logger;
@ApplicationScoped
public class SocialMedia implements Consumer<News> {
private static final Logger LOGGER = Logger.getLogger(SocialMedia.class.getName());
@Override
public void accept(@Observes News news) {
LOGGER.info("We got the news, we'll publish it on Social Media: " + news.get());
}
}
5. Create the Journalist
class
Steps
- Create a class called
Journalist
in theexpert.os.labs.persistence.cid.news
package - Annotate it with
@ApplicationScoped
from thejakarta.enterprise.context
package - Create a private
Event<News>
field and annotate it with@Inject
fromjakarta.inject
package- The
Event
is from thejakarta.enterprise.event
package
- The
- Create a method named
receiveNews
with aNews
parameter which will fire and event (this.event.fire()
)
Solution
Click to see...
6. Create the app class
Steps
- Create a class called
AppJournalist
in theexpert.os.labs.persistence.cid
package -
Add a main method
-
Set up a try-with-resources block, inside the
main
method, to manage the Jakarta EESeContainer
that is responsible for dependency injection and managing resources -
Obtain the value of the
Journalist
through the container instance - Printout the news using the method
receiveNews
from theJournalist
using theNews.of()
Expected results
-
The following output
Solution
Click to see...
import expert.os.labs.persistence.cdi.news.Journalist;
import expert.os.labs.persistence.cdi.news.News;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
public class AppJournalist {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Journalist journalist = container.select(Journalist.class).get();
journalist.receiveNews(News.of("Java 21 has arrived!!"));
}
}
}