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

Edwin Steiner esteiner at inventage.com
Sun Sep 2 08:45:28 EDT 2018


Hello Dmitry

Thank you very much for your reply.

Unfortunately it didn’t solve my problem. I think it is because I want to access the EntityManager of the Keycloak persistence unit itself. This persistence unit is defined in the keycloak-model-jpa JAR referenced by the module "org.keycloak.keycloak-model-jpa” and it is named “keycloak-default”. I can not access it, because it is not exposed via JNDI (e.g. with <property name="jboss.entity.manager.jndi.name" value="java:/yourEntityManagerName"/>).

We use the Keycloak SPI org.keycloak.connections.jpa.entityprovider.JpaEntityProviderFactory to persist our own JPA entities.

More precisely my question is the following: is there a way to get access to the Keycloak EntityManager in a MDB or EJB within a custom deployment? 

Best regards,
Edwin

-- 
Edwin Steiner
phone +41 43 343 20 22 | mobile +41 79 441 57 56
Inventage AG | CH-8005 Zürich | www.inventage.com

> On 28 Aug 2018, at 01:20, Dmitry Telegin <dt at acutus.pro> wrote:
> 
> 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