I have Jakarta EE project (Jetty embedded based version 11) with JPA, CDI, JMS & JAX-RS project.
Everything works good, however I want to add a new scope
@Inherited
@NormalScope
@Retention(RetentionPolicy.RUNTIME)
public @interface MyScoped {}
I have also the context of the scope
import jakarta.enterprise.context.spi.Context;
import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MyScopeContext implements Context {
private final Map<Bean<?>, Object> instances = new ConcurrentHashMap<>();
private final BeanManager beanManager;
public MyScopeContext(BeanManager beanManager) {
this.beanManager = beanManager;
}
@Override
public Class<? extends Annotation> getScope() {
return MyScoped.class;
}
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
System.out.println("FLAG1");
@SuppressWarnings("unchecked")
Bean<T> bean = (Bean<T>) contextual;
return (T) instances.computeIfAbsent(bean, b -> beanManager.getContext(bean.getScope()).get(bean, creationalContext));
}
@Override
public <T> T get(Contextual<T> contextual) {
System.out.println("FLAG2");
@SuppressWarnings("unchecked")
Bean<T> bean = (Bean<T>) contextual;
return (T) instances.get(bean);
}
@Override
public boolean isActive() {
return true
}
// Method to clean up instances when the context is destroyed
public void destroy() {
instances.clear();
}
}
I’ve activate the context hard coded to make it work.
And this is the Extension:
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.*;
public class MyScopeExtension implements Extension {
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager manager) {
BeanManager beanManager = CDI.current().getBeanManager();
abd.addContext(new MyScopeContext(beanManager));
}
void addScope(@Observes final BeforeBeanDiscovery beforeBeanDiscovery) {
beforeBeanDiscovery.addScope(MyScoped.class, true, false);
}
BeanManager beanManager = CDI.current().getBeanManager();
afterBeanDiscovery.addContext(new MyScopeContext(beanManager));
}
}
I have also simple bean to demonstrate it all together:
@MyScoped
public class TempBean {
public String say() {
return "Hello world";
}
}
Final step, I added the file jakarta.enterprise.inject.spi.Extension
in resources/META-INF/services
with the content io.shaikezam.scope.MyScopeExtension
Now, when I build the and run the project I get:
org.jboss.weld.contexts.ContextNotActiveException: WELD-001303: No active contexts for scope type io.shaikezam.scope.MyScoped
I can see that the jakarta.enterprise.inject.spi.Extension
file in the builded jar is in classes/META-INF/services/
and not in META-INF/services/
, maybe it’s related?
The is the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.shaikezam</groupId>
<artifactId>jakarta-system-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>product-service</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
</dependency>
<dependency>
<groupId>jakarta.jms</groupId>
<artifactId>jakarta.jms-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
<dependency>
<groupId>io.shaikezam</groupId>
<artifactId>utils</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>