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
newsin theexpert.os.labs.persistence.cidpackage - Create the class
Newsin theexpert.os.labs.persistence.cid.news - Implements the
Supplier<String>interface and it's methodget() - Add a private
Stringfield 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 thenameattribute
Solution
Click to see...
2. Create the MAgazine class
Steps
- Create a class called
Magazinein theexpert.os.labs.persistence.cid.newspackage - Implements the
Consumer<News>and the methodaccept()- the
Consumerclass is from thejava.util.functionpackage
- the
- Define a private attribute
Loggerfrom thejava.util.loggingpackage - Add the
@Observesannotation to theNewsattribute in theaccept()method, where it's from thejakarta.enterprise.eventpackage - Log the
Newsin 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
NewsPapperin theexpert.os.labs.persistence.cid.newspackage - Annotate it with
@ApplicationScopedfrom thejakarta.enterprise.contextpackage - Implements the
Consumer<News>and the methodaccept()- the
Consumerclass is from thejava.util.functionpackage
- the
- Define a private attribute
Loggerfrom thejava.util.loggingpackage - Add the
@Observesannotation to theNewsattribute in theaccept()method, where it's from thejakarta.enterprise.eventpackage - Log the
Newsin 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
SocialMediain theexpert.os.labs.persistence.cid.newspackage - Annotate it with
@ApplicationScopedfrom thejakarta.enterprise.contextpackage - Implements the
Consumer<News>and the methodaccept()- the
Consumerclass is from thejava.util.functionpackage
- the
- Define a private attribute
Loggerfrom thejava.util.loggingpackage - Add the
@Observesannotation to theNewsattribute in theaccept()method, where it's from thejakarta.enterprise.eventpackage - Log the
Newsin 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
Journalistin theexpert.os.labs.persistence.cid.newspackage - Annotate it with
@ApplicationScopedfrom thejakarta.enterprise.contextpackage - Create a private
Event<News>field and annotate it with@Injectfromjakarta.injectpackage- The
Eventis from thejakarta.enterprise.eventpackage
- The
- Create a method named
receiveNewswith aNewsparameter which will fire and event (this.event.fire())
Solution
Click to see...
6. Create the app class
Steps
- Create a class called
AppJournalistin theexpert.os.labs.persistence.cidpackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain the value of the
Journalistthrough the container instance - Printout the news using the method
receiveNewsfrom theJournalistusing 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!!"));
}
}
}