Search
Calendar
September 2017
S M T W T F S
« Sep    
 12
3456789
10111213141516
17181920212223
24252627282930
Your widget title
Archives

Posts Tagged ‘Jndi’

PostHeaderIcon Use a JDBC datasource from WebLogic’s JNDI

Case

Your application is connected to a database. The configuration of the DB connexion is set in a Spring file. You would like the connexion to be set in WebLogic, so that no URL/login/password lays in your source code.

Steps

WebLogic

Create a new data source:

  • go the WebLogic console >
    • JDBC >
      • DataSource >
        • New >
          • set the name, JNDI name (eg: database.jndi.name), DB type  (Oracle, Sybase, …) >
            • Next >
              • Select the driver >
                • Next >
                  • Select the targeted server(s)

This will

  1. create a new file <yourDomain>/config/jdbc/<datasourceName>-XXXX-jdbc.xml.
  2. add a <jdbc-system-resource> block in <yourDomain>/config/config.xml

Spring

Replace the block:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
 </bean>

with:

<jee:jndi-lookup id="dataSource" jndi-name="${database.jndi.name}" />

PostHeaderIcon Deploy a JMS destination queue with Spring

Abstract

Case: we have to send JMS messages to a third-party server, using Spring. Of course, we have to discriminate production, UAT and developments environments. We can decide to use one Spring configuration file per environment, but it is complex to maintain.

Here is the way I proceded:

Declare the factory

<bean id="myJmsQueueConnectionFactory">
    <property name="jndiName" value="my.jms.QueueConnectionFactory"/>
   <property name="jndiTemplate" ref="myJmsJndiTemplate"/>
 </bean>

Declare a Jndi Template

<bean id="myJmsJndiTemplate">
   <property name="environment">
     <props>
       <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
       <prop key="java.naming.provider.url"><strong>${my.jms.host}</strong></prop>
     </props>
   </property>
 </bean>

The variable ${my.jms.host} is used to indicate the actual destination host. The value is given in a property file myConfig.properties, for instance:

my.jms.host=t3://127.0.0.1:1234

Obviously, each environnement needs its ad hoc property file!

Declare a JMS Template

It gathers the factory and the queue name.

<bean id="myJmsTemplate">
  <property name="connectionFactory" ref="myJmsQueueConnectionFactory"/>
  <property name="defaultDestination" ref="myJmsQueue"/>
 </bean>

Declare the destination queue

<bean id="myJmsQueue">
 <property name="jndiName" value="my.jms.destination.queue.name"/>
</bean>

Ensure the property file is in classpath

<bean id="conf">
   <property name="locations">
   <list>
     <value>classpath*:myConfig.properties</value>
   </list>
  </property>
 </bean>

Restart WebLogic, deploy your EAR. Now it should work!

Notice: in case you’re not sure the distant host is up or not, you may add this attribute to your beans: lazy-init="true"