JNDI lookup on Glassfish 4.1.1 custom resources

2019-08-01 02:33发布

问题:

I am trying to use JNDI custom resources to store data directly in my server (Glassfish) but it is apparently not working.

I define my JNDI as on the image below

which corresponds to the following description in my domain.xml.

<custom-resource factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory" res-type="java.lang.String" jndi-name="projectStage">
      <property name="value" value="UnitTest"></property>
</custom-resource>

And then, i am trying to get this value in one of my managed bean.

Context c = new InitialContext();
c.lookup("projectStage");

But... javax.naming.NamingException: Lookup failed for 'projectStage' is the only thing i get from it.

If anyone has an idea for me, i will be thankful !

回答1:

Solved !

I was editing my domain.xml manually to add my JNDI custom resource as Glassfish 4.1.1 administration interface gives an Runtime error when clicking on New... on the JNDI Custom Resource section.

Once the domain.xml file edited, it was displayed well in the administration panel but apparently not properly working.

I have created the custom resource with the create-custom-resource command of the asadmin console explains here.

create-custom-resource --restype java.lang.String --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property "value=UnitTest" projectStage

And then it looks to be properly added to the system. (looks like it is not only stored in the domain.xml file)



回答2:

I got a similar problem when transfering configuration manually between an older glassfish version and Glassfish 4.1.

Your suggested solution do work but it is a lot of work if you have a lot of properties. But adding it via asadmin and comparing domain.xml I found the reason for the failure.

If you just add custom-resource glassfish wont be able to find it. It also need to have a reference to the resource. In my case it was in the <server> part, which needed a

<resource-ref ref="projectStage"></resourceRef>