Wednesday, November 11, 2009

How to access JNDI from Spring's Application Context

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MyDataSource"/>
</bean>

<bean id="userDao" class="com.foo.JdbcUserDao">
<!-- Spring will do the cast automatically (as usual) -->
<property name="dataSource" ref="dataSource"/>
</bean>

Tuesday, November 10, 2009

Spring JMS message consumer ( JBoss MQ )

  1. Create a new web project in eclipse.
  2. Under the WEB-INF/lib folder copy the following jar files
    1. commons-logging.jar
    2. spring.jar
    3. spring-jms.jar
  3. Create a listener bean
  4. package com.test.jms.pojo;
    
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;
    
    public class Listener1 implements MessageListener {
    
     public void onMessage(Message message) {
      if (message instanceof TextMessage) {
       try {
        System.out.println(((TextMessage) message).getText());
       } catch (JMSException ex) {
        throw new RuntimeException(ex);
       }
      } else {
       throw new IllegalArgumentException(
         "Message must be of type TextMessage");
      }
    
     }
    
    }
  5. Create Spring configuration file.
  6. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans"
     xsi:schemalocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     <bean class="com.test.jms.pojo.Listener1" id="listener1">
      <bean
       class="org.springframework.jms.listener.DefaultMessageListenerContainer"
       id="jmsContainer">
       <property name="connectionFactory" ref="connectionFactory"></property>
       <property name="destination" ref="c8queue"></property>
       <property name="messageListener" ref="listener1"></property>
      </bean>
      <bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
       <property name="connectionFactory" ref="connectionFactory"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="c8queue">
       <property name="jndiName" value="queue/c8queue"></property>
       <property name="jndiTemplate" ref="jndiTemplate"></property>
       <property name="resourceRef" value="false"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="connectionFactory">
       <property name="jndiName" value="ConnectionFactory"></property>
       <property name="jndiTemplate" ref="jndiTemplate"></property>
       <property name="resourceRef" value="false"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiTemplate" id="jndiTemplate">
       <property name="environment">
        <props>
         <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory </prop>
         <prop key="java.naming.provider.url">jnp://localhost:1099</prop>
         <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces </prop>
        </props>
       </property>
      </bean>
     </bean>
    
  7. Create queue in Jboss
    1. In folder $JBOSS_HOME/server/default/deploy/jms create a file yourProj-service.xml
    2. <server>
       <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=c8queue">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager </depends>
        <attribute name="MessageCounterHistoryDayLimit">-1</attribute>
        <attribute name="SecurityConf">
         <security>
          <role name="guest" read="true" write="true">
           <role create="false" name="publisher" read="true" write="true">
            <role create="false" name="noacc" read="false" write="false"></role>
           </role>
          </role>
         </security>
        </attribute>
       </mbean>
      </server>
      
  8. Edit web.xml to load spring configuration
  9. <web-app id="WebApp_ID" version="2.5"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <display-name>tstSprJms</display-name>
     <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>WEB-INF/applicationContext.xml</param-value>
     </context-param>
     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
    </web-app>
    
  10. Restart the server

    Monday, November 9, 2009

    Wedding shopping in Delhi

    Two weeks back me and my bro. were shopping for my wedding, I earlier decided to wear suit but then after discussion with my friends I finalized to sherwani, looking for suit for my bro we went though a lot of shops in karol bagh but we didn't find anything that justified the price. we then shifted our focus towards CP, were the collection of suits was nice and we could easily finalize suit for sumit. for sherwani my friends suggested chandini chowk, we went there and found a good collection of sherwanis, most of the shops were quite congested with little or no space to even change but the level of bargining that you do was amazing, for instance we finalized on a sherwani that the deler was quoting to be as Rs 12500, and will some barganing we were able to buy the same sherwani for Rs 5000. So whenever you are looking forward to shop for sarees, lahenga, sherwani, etc. Chandini Chowk is the place to look for.
    and if you plan to go there don't miss the paranthe wali gali and natraj ke dhahi bhalle :)

    Sunday, November 8, 2009

    Karmic Koala - a worth upgrade.

    Recently I upgraded my ubuntu from 9.04 to 9.10, and as always I am really happy after installing the new verison, its looks have improved, even the console is very beautifull, hardware support for Nvidia cards can be seen right from the installation screen. overall I would say that it is worth spending few hrs and upgrade.

    Thursday, November 5, 2009

    Mata Vaishno Devi Yatra 09

    As every year Maa called us this year also, since so many years every yatra has been memorable but this one had interesting experiences. One day before we boarded on the train on Thursday, 22nd Oct evening there were few cancellations from the people who were expected to travel with us this may sound strange but over the years I have seen this is a normal trend that happens when we plan 2 months in advance. varun on the other hand planned to join us at the eleventh hour. even the start was shaky as most of our tickets got confirmed the same day we were not sure about our coatch. but some how we found our seats and the train in an overnight journey took us to Jammu, I was surprised to see a lot of people in karta as this being not a weekend travel I was expecting much less traffic. we quickly got ready in keshav ashram, and started yatra, I was keenly looking forward towards baan ganga but it was a disappointing scene to find very less water in the stream, which draws us to a conclusion that one should not prefer planning yatra on Oct , as far as I remember I have seen ample of water when I used to come around Nov, Feb, March. I had a new camera and enjoyed taking a lot of pics all the way up and down the hill, a silly mistake from me was enough for 4 of us to miss the train (on our way back to delhi) in the end, and it will be a memorable evening how we struggled all night to find some place to sleep for few hours.