[keycloak-user] Access to EntityManager/KeycloakSession from a MessageDrivenBean inside Keycloak

Dmitry Telegin dt at acutus.pro
Mon Aug 27 19:20:39 EDT 2018


Hello Edwin,

Keycloak does support stateful EJBs as extensions: https://www.keycloak.org/docs/latest/server_development/index.html#leveraging-java-ee

These EJBs are able to obtain an EntityManager as follows, without specifying unitName:

    @PersistenceContext
    protected EntityManager em;

Could you try the same in your MDB? If that doesn't work, I think you could create a dummy stateful EJB whose sole purpose would be to propagate EntityManager, KeycloakSession etc. to your MDB (via e.g. JNDI).

Good luck!
Dmitry Telegin
CTO, Acutus s.r.o.
Keycloak Consulting and Training

Pod lipami street 339/52, 130 00 Prague 3, Czech Republic
+42 (022) 888-30-71
E-mail: info at acutus.pro

On Sun, 2018-08-26 at 16:19 +0200, Edwin Steiner wrote:
> Hello All
> 
> We would like to extended our Keycloak instance with a MessageDrivenBean for user synchronization. Inside the MessageDrivenBean we need access to the EntityManager for storing the imported users in the database.
> 
> @MessageDriven(name = "ImportUserMessageHandler", activationConfig = {
>         @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
>         @ActivationConfigProperty(propertyName = "destination", propertyValue = “ImportQueue"),
>         @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
> @ApplicationScoped
> public class ImportUserMessageHandler extends AbstractMessageDrivenBean {
> 
>     @PersistenceContext(unitName="keycloak-default")
>     private EntityManager entityManager;
> 
> }
> 
> But the above code results in the following error message:
> 
> ————————
> Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEE0041: Component class com.inventage.iam.import.ImportUserMessageHandler for component ImportUserMessageHandler has errors: 
> WFLYJPA0033: Can't find a persistence unit named keycloak-default in deployment "keycloak-spi.jar"
> 	at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor$1.handle(ModuleJndiBindingProcessor.java:157)
> 	at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:54)
> 	at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.processClassConfigurations(ModuleJndiBindingProcessor.java:186)
> 	at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:143)
> 	at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:165)
> 	... 5 more
> ————————
> 
> Normally the EntityManager is gotten via keycloakSession.getProvider(JpaConnectionProvider.class).getEntityManager(), but in the MessageDrivenBean we have no access a KeycloakSession instance.
> 
> Is there an other way to get the EntityManager? Or can we get somehow a KeycloakSession instance inside the MessageDrivenBean?
> 
> Thanks
> Edwin
> 
> 
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user


More information about the keycloak-user mailing list