google Analytics

Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Thursday, October 7, 2010

JtaTransactionManager Bean

JtaTransactionManager Bean
=============================================================
 


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
...
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager"/>
...
</beans>

By default, the JtaTransactionManager will attempt to automatically detect a UserTransaction
bound to the java:comp/UserTransaction JNDI name; if the java:comp/UserTransaction object also
implements the TransactionManager, the JtaTransactionManager will use that; if the java:comp/
UserTransaction does not implement the TransactionManager, the JtaTransactionManager will attempt
to find the TransactionManager under these JNDI names:
  1. • java:comp/UserTransaction: The default JNDI name, used by Resin 2.x, Oracle OC4J (Orion),
  2. JOnAS (JOTM), BEA WebLogic, and IBM WebSphere
  3. • java:pm/TransactionManager: JNDI name for TransactionManager in Borland Enterprise Server and Sun Application Server (Sun ONE 7 and later)
  4. • java:comp/TransactionManager: JNDI name used in Resin 3.x
  5. • java:/TransactionManager: The name used in the JBoss Application Server

===========================================================================================



jtatransactionmanager example
jtatransactionmanager spring example
jtatransactionmanager hibernate
ANish

Wednesday, September 15, 2010

Spring 3.0 @Controller Overview

@Controller
Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications,
through the @PathVariable annotation and other features
Spring will use any class annotated with the @Controller annotation as a Controller in Spring MVC support.


=============================================================
 

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 *
@author NathA1
Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications,
through the @PathVariable annotation and other features
Spring will use any class annotated with the @Controller
annotation as a Controller in Spring MVC support.
 */
 

@Controller
/**
The @RequestMapping annotation is used to map URLs to the Controller class or aparticular method
If you want to use the @RequestMapping annotation at the method level,
you need to configure DefaultAnnotationHandlerMapping. This is done
by default for DispatcherServlet, but if you are implementing your own handler adapter, you will
have to define AnnotationMethodHandlerAdapter

@RequestMapping Configuration  in Spring

<?xml version="1. 0" encoding="UTF-8" ?>
<context: component-scan base-package="com.test.web" />
<bean class="org.springframework.web.➥
servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org. springframework. web. ➥
servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

 */
@RequestMapping(value="/account")

public class AccountController {

    private Map<Long, Account> accounts = new ConcurrentHashMap<Long, Account>();
   
    @RequestMapping(method=RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new Account());
        return "account/createForm";
    }
   
    @RequestMapping(method=RequestMethod.POST)
    public String create(@Valid Account account, BindingResult result) {
        if (result.hasErrors()) {
            return "account/createForm";
        }
        this.accounts.put(account.assignId(), account);
        return "redirect:/account/" + account.getId();
    }
   
    @RequestMapping(value="{id}", method=RequestMethod.GET)
    public String getView(@PathVariable Long id, Model model) {
        Account account = this.accounts.get(id);
        if (account == null) {
            throw new ResourceNotFoundException(id);
        }
        model.addAttribute(account);
        return "account/view";
    }

}

==========================================================================


ANish

Saturday, August 21, 2010

Job Scheduling with Spring

Job Scheduling with Spring
Scheduling support in Spring comes in two distinct forms: JDK Timer-based and Quartz-based.
The JDK Timer-based approach provides scheduling capabilities on any version 1.3 or later JVM,
and it does not need external dependencies beyond Spring. Timer-based scheduling is quite primitive
and provides limited flexibility when defining job schedules. However, Timer support is included
with Java and requires no external libraries, which might be beneficial if you are restricted by application
size or corporate policy. Quartz-based scheduling is much more flexible and allows triggers
to be defined in a much more real-world way, such as the earlier example of 3:00 p.m. every Monday,
Wednesday, and Friday

Scheduling Jobs Using JDK Timer
Creating a Simple Job
To create a job to use with the Timer class, you simply extend the TimerTask class and implement the
run() method to execute your job’s logic

public class HelloWorldTask extends TimerTask {
public void run() {
System.out.println("Hello World!");
}
}

import java.util.Timer;
public class OneOffScheduling {
public static void main(String[] args) {
Timer t = new Timer();
t.schedule(new HelloWorldTask(), 1000);
}
}

=============================================
Scheduling a Repeating Task
public class FixedDelayScheduling {
public static void main(String[] args) throws Exception{
Timer t = new Timer();
t.schedule(new HelloWorldTask(), 1000, 3000);
}
}

import java.util.Timer;
public class FixedRateScheduling {
public static void main(String[] args) throws Exception {
Timer t = new Timer();
t.scheduleAtFixedRate(new HelloWorldTask(), 1000, 1000);
}
}
=====================================
Spring Support for JDK Timer Scheduling
The core of Spring’s Timer support comes in the form of the ScheduledTimerTask and
TimerFactoryBean classes. The ScheduledTimerTask class acts as a wrapper around your TimerTask
implementations and allows you to define trigger information for the job. Using the TimerFactoryBean,
you can have Spring automatically create Timer instances for a given list of ScheduledTimerTask beans
using the trigger configuration data when creating the trigger

Configuring Job Scheduling Using TimerFactoryBean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="job" class="com.apress.prospring2.ch12.timer.HelloWorldTask"/>
<bean id="timerTask"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay" value="1000" />
<property name="period" value="3000" />
<property name="timerTask" ref="job" />
</bean>
<bean id="timerFactory"
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="timerTask"/>
</list>
</property>
</bean>
</beans>

=================================
Scheduling Jobs Using OpenSymphony Quartz
Quartz provides a huge range of features such as persistent jobs, clustering,
and distributed transaction,The core of Quartz is made up of two interfaces, Job and Scheduler, and two classes, JobDetail
and Trigger


Scheduling jobs using Quartz or Timer
Dynamic Job Scheduling using Spring
Scheduling Jobs Using OpenSymphony Quartz


Monday, August 9, 2010

MDB Using SpringBeanAutowiringInterceptor EJB3.0 with JBOSS Example

=============================================================

=============================================================
@MessageDriven(
activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="testqueue")
})
@Interceptors(SpringBeanInterceptors.class)
public class TestMDB implements MessageListener {
public TestMDB() {
System.out.println("Inside TestMDB()");
}
@Autowired
private AnnotatedTarget target;

public void onMessage(Message message) {
System.out.println("onMessage-->>"+message);

}

}

=============================================================

=============================================================
==================================================================================================================================
public class SpringBeanInterceptors extends SpringBeanAutowiringInterceptor {

=============================================================

=============================================================
private static final String DEFAULT_RESOURCE_LOCATION = "classpath*:beanRefContext.xml";
@Override
protected BeanFactoryLocator getBeanFactoryLocator(Object target) {
System.out.println("Called BeanFactoryLocator2->>>>>>>>>>>>>>>>>>>>>>>>>>>>");
return ContextSingletonBeanFactoryLocator
.getInstance(DEFAULT_RESOURCE_LOCATION);
}

}
======================================================

==================================================================================================================================
public class AnnotatedTarget {
@Autowired
private Foo foo;
@Autowired
private Foo foo2;
@Autowired
private Bar bar;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("AnnotatedTarget");
sb.append("{foo=").append(foo);
sb.append(", foo2=").append(foo2);
sb.append(", bar=").append(bar);
sb.append('}');
return sb.toString();
}
}
==================================================================================================================================

=============================================================

=============================================================
public class Bar {

@Override
public String toString() {
return getClass().getName();
}
}
==================================================================================================================================
public class Foo {

@Override
public String toString() {
return getClass().getName();
}
}

=============================================================

=============================================================
==================================================================================================================================
META-INF\SpringContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="com.apress.prospring2.ch03.autowiring.Foo"/>
<bean id="bar" class="com.apress.prospring2.ch03.autowiring.Bar"/>
<bean id="annotatedTarget" class="com.apress.prospring2.ch03.autowiring.AnnotatedTarget"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
==================================================================================================================================
--
in Ear folder outside META-INF
beanRefContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanfactoryTDS"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="META-INF/SpringContext.xml" />
</bean>
</beans>

=============================================================

=============================================================
==================================================================================================================================
Create EAR

Spring with ejb3.0
ANish