Showing posts with label j2EE. Show all posts
Showing posts with label j2EE. Show all posts

Reading Messages from JMS Queue in Weblogic 12.1.1 using Java

Suppose I have Queue configured in Weblogic and I want to write application in which Program A will put messages to this queue and Program B will read data from this Queue.

 There are multiple Persistence Store options availble in WL like The file or database in which this JMS server stores persistent messages. If unspecified, the JMS server uses the default persistent store that is configured on each targeted WebLogic Server instance As part of below example We will be using default one.

In order to retrieve messages from queue, you have should queue containing messages.
Please refer to previous article to send messages to JMS Queue.

Sending Message to JMS Queue in Weblogic 12.1.1 using Java

Suppose I have Queue configured in Weblogic and I want to write application in which
Program A will put messages to this queue and Program B will read data from this Queue.

There are multiple Persistence Store options availble in WL like The file or database in which this JMS server stores persistent messages.If unspecified, the JMS server uses the default persistent store that is configured on each targeted WebLogic Server instance. As part of below example We will be using default one.

Retrieving Data from DataSource Configured in WebLogic 12.1

Suppose I have MySQL Database called RetailStore and have table Order containing order related data which customer submitted via online applications. Our requirement is to write java program which will read data from DataSource which is configured in Weblogic for our MYSQL Database.

You need to follow below steps.
  1. Create DataSource in WebLogic. I have used MySQL as DB and Weblogic 12.1.1 as Application Server.
  2. Test DataSource in weblogic
  3. Create HashMap consisting Initial Context Factory and Provider URL
  4. Create initial context Factory
  5. Do Lookup on context using JNDI configured for DataSource and have dataSource instance
  6. Get Connection from DataSource and Perform common JDBC Logic to read data from Table.

DataSource in WebLogic
Go to Services -> DataSource-> Create new DataSource. you will be asked to follow series of steps.
If you have MYSQL Db then please provide below configuration

Solution : Exception "\IBM\WebSphere was unexpected at this time"

While I was developing webservices, I was unable to start Weblogic configured in Netbeans and getting exception "\IBM\WebSphere was unexpected at this time".

Following steps I have performed but issue doesn't resolved
  1. Removed Weblogic in Netbeans But issue doesn't solved
  2. Removed Netbeans and done fresh installation thinking Might be Netbeans Bug But issue doesn't solved
  3. Removed Weblogic installation and reinstalled Weblogic fresh installation But Issue doesn't solved
  4. Then I was thinking why issue doesn't got resolved.

Building Application with Spring Boot

Spring Boot makes very easy to build standalone as well as production grade Spring based application. I found features like "Embedded Tomcat and Jetty. no need to deploy war. also XML based configuration eliminated" - interesting !

Spring Boot Features :
  • Create Stand Alone Spring based Application
  • No need for XML Configuration
  • Embedded Tomcat and Jetty (no need to deploy war)
  • Configure Spring Automatically whenever possible
  • Maven Configuration simplified
  • Provide production ready features like health check and externalized Configuration.

Hibernate Named Queries Example

Hibernate provides @NamedQueries template using which you can associate unique query name to query.
Since Named Queries are global access , their name should be unique. You can use Named Queries in XML Mappings or using Annotation.

If You are using XML instead of annotation then you can add Named Queries as below in Hibernate hbm.xml file. It should be after Class tags.

       <![CDATA[from Customer cust where cust.customerType = :customerType]]>


What is Javaassist?

Javaassist stand for Java Programming Assistance. It's java library providing means to manipulate java bytecode. 
  • It enables java program to create new java classes at runtime and to modify classes before JVM loads it.
  • It's sub project of Jboss.

Common Aware Interfaces used in Spring

If You are developing application using Spring then sometime it's important that your beans aware about Spring IOC container resources. Here, Spring Aware comes into picture.

To use Aware Interfaces, You bean class should implement aware interfaces. and That's way they will be aware about container resources. I like to use these interfaces :)

Common Aware Interfaces used in Spring 3.2:
  1. BeanNameAware
  2. ApplicationContextAware
  3. BeanFactoryAware
  4. MessageSourceAware
  5. ApplicationEventPublisherAware
  6. ResourceLoaderAware

Spring @PostConstruct and @PreDestroy annotation Example

If You want to perform specific action or handling upon bean Initialization and Bean destruction then Implementing InitializingBean and Disposable Bean or init-method and destroy-method is for You !!

But there is another annotation approach available. You need to annotate methods with @PostConstruct and @PreDestroy annotation in you bean class and define in Spring Bean configuration file.

You can either add CommonAnnotationBeanPostProcessor (org.springframework.context.annotation.CommonAnnotationBeanPostProcessor) class in Spring bean xml or use context:annotation-config in spring bean configuration file

Init-method and Destroy-method Spring Bean example

If You want to perform specific action or handling upon bean Initialization and Bean destruction then Implementing InitializingBean and Disposable Bean is for You !!

But there is another approach also available if you don't want to implement those interfaces..You can add init-method and destroy-method into your spring bean configuration file at bean level.

Difference between context:component-scan and context:annotation-config in Spring

<context:component-scan/> scan packages and classes within given base packages then find and register beans into ApplicationContext.It does all things that <context:annotation-config/> is supposed to do.
So if you have used annotation for example, @Autowired in your code and <context:component-scan/> in xml then You do not require to use <context:annotation-config/>

What is ApplicationContext and BeanFactory in Spring?

ApplicationContext(org.springframework.context.ApplicationContext) is interface provided by Spring  and it's used for getting Configuration information about your application.Spring has many classes which implements this interface.

What are common Implementation of ApplicationContext ? - They are
  1. ClassPathXmlApplicationContext
  2. FileSystemXmlApplicationContext
  3. XmlWebApplicationContext

Spring InitializingBean and DisposableBean Example

In Spring, If You want to perform specific actions on Bean Initialization or Destruction then Spring abstract Interface InitializingBean and DisposableBean are for You !

InitializingBean is abstract interface which has abstract method called afterPropertiesSet. If You have any bean which implements this interface then after all bean properties are set, this afterPropertiesSet method will be called.

DisposableBean is abstract Interface which has abstract method called destroy. If You have any bean which implements this interface then after bean is released by Spring container, this method will be called.

How to Track Session Attribute - HttpSessionAttributeListener Example

If You want to track session Attribute like whether there is new session attribute added,removed or replaced during application lifecycle then HttpSessionAttributeListener is for You !

HttpSessionAttributeListener is abstract interface which comes as part of servlet-api.jar which has abstract method such as attributeAdded,attributeRemoved,attributeReplaced.

How it works : 
  • When new session attribute is added using set then attributeAdded will be called.
HttpSession session = request.getSession();
session.setAttribute("sessionName","LoginIn");
  • If you try to set attribute value to some different value which is already present then attributeReplaced will be called. ex. session.setAttribute("sessionName","LoggedIn");

How to Calculate Total Active Session - HttpSessionListener Example

If you have web application and you want to count total number of active session then HttpSessionListener is for you !

HttpSessionListener is abstract interface provided as part of servlet-api.jar which has abstract method such as sessionCreated and sessionDestroyed which get executed every time when we create new session using httpSession or invalidate session.

By default one session is created. so when you use HttpSession session = request.getSession(); in HttpServlet then it will give you that session. You can invalidate session using session.invalidate();

Listener - ServletContextListener Example

If You want to do something before application starts, then ServletContextListener is for you.

ServletContextListener is abstract interface provided as part of servler-api.jar which provides contextDestroyed and contextInitialized abstract method. One can create class which implements ServletContextListener and write implementation logic for example, logic that does something before web application starts.

How to Download File using Servlet

Consider that I have file containing project information available into server and I want to download that file. Using webservices you can also achieve same but Here, I am going to discuss "Download File using Servlet"

Steps to be Performed to Download File using Servlet :
  1. Create Servlet File which will download file available in server
  2. Add Servlet entry into Web.xml or annotate with @WebServlet 

javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found

I was working on writing JSR 303 Custom Validation to validate Email Address and encountered with following exception.

javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
    at com.anuj.core.test.ValidEmailTest.setUp(ValidEmailTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

How to write Custom JSR 303 Validation using ConstraintValidator

What is JSR 303?
JSR 303 is Java Bean Validation framework which comes as part of J2EE 6. So in order to use classes specified by them, for Validation you need to include javaee-api jar Ex. javaee-api-7.0.jar

Requirement :
Consider that I want to write my own custom validation to validate email. Ya, there is already email validator provided by Hibernate as part of Hibernate 4.2 but this is just for eample. and you can create own validation to validate FileName or any other validation you like.

Steps to create Custom Validation to validate Email :
1. Create Interface - @interface
2. Create Custom Validation Implementation class using ConstraintValidator
3. Apply Custom Annotation to Bean(Pojo) but it can be any where on Constructor,Method,parameter,Field etc based on how you have added ElementType in @Target

EJB 3.1 SessionBean Example with Remote Interface using WebLogic 12.1.1

EJB stands for Enterprise Java Bean. If you are not fimilar with EJB, Please read below in order to move ahead with "Creating EJB 3.1 Application using WebLogic 12.1.1".
  1. What is EJB and Why Comes into Picture
  2. EJB Container and it's Feature
EJB application requires to be run in Application Server like WebLogic, JBoss. As part of current EJB Demo, WebLogic Server 12.1.1 and  JDK 1.7 Update 03 is used.

Suppose, I want to write Stateless Session Bean(Can be local or remote), in My case Remote Component which will provide customer Information upon request from Client.