Wednesday, November 18, 2009

Spring start up servlet

here is a link to a good solution.
http://forum.springsource.org/showthread.php?t=55656

In case this blog content ever gets deleted here are the contents.

www.springsource.com
SpringSource basic
Community SpringSource Projects Downloads Documentation Forums Training Exchange Blogs

Go Back Spring Community Forums > Core Spring Projects > Web
Reload this Page load servlet on startup

User Name Remember Me?
Password
Register FAQ Calendar Today's Posts Search

Search Forums
Show Threads Show Posts
Tag Search
Advanced Search
Go to Page...
Reply

Thread Tools Display Modes
#1
Old Jun 9th, 2008, 06:19 PM
imbali imbali is offline
Junior Member

Join Date: Jun 2008
Posts: 1
Default load servlet on startup
Hi, I´m really new to the Spring Framework, so I might be making some very silly questions, but here goes:

Im working on an existing application that uses the Spring Framework. So here´s the thing, I would like to read some data from the db and perform some alterations to some of the data in it, but I wanted to do this when the web application was loaded.

I was thinking of creating a servlet that would deal with the alterations to the db and then in the web.xml define the "load-on-startup" and putting a smaller value than the value indicated to load the dispatcher servlet. So here´s my problem, I wont be able to access the db before the dispatcher servlet because then I wont be able to access the db, because all the hibernate configurations, datasource configuration are all done in the dispatcher serlvet.
Reply With Quote
imbali
View Public Profile
Send a private message to imbali
Find all posts by imbali
#2
Old Jul 8th, 2008, 07:15 PM
jfredrickson jfredrickson is offline
Junior Member

Join Date: Mar 2008
Posts: 10
Default
I know this reply is coming a month late, so I hope it's still of some use to you or anyone who comes along with the same question...

You could create a bean that handles your startup actions, then define that bean in your servlet config. Off the top of my head, it'd be something like:

Code:



Then perform your startup actions (DB updates, etc) in AppInit's startup method. As long as your servlet is configured to load on startup in web.xml, this should work.
Reply With Quote
jfredrickson
View Public Profile
Send a private message to jfredrickson
Find all posts by jfredrickson
#3
Old Jul 17th, 2008, 09:32 AM
brismith brismith is offline
Junior Member

Join Date: Mar 2008
Location: Taylors, SC USA
Posts: 8
Default
Great tip. Thanks.
Reply With Quote
brismith
View Public Profile
Send a private message to brismith
Find all posts by brismith
#4
Old Jul 17th, 2008, 10:21 AM
gregd gregd is offline
Member

Join Date: Nov 2005
Location: Athens, Greece
Posts: 76
Default
I think a BeanFactoryPostProcessor should be a better approach.
Otherwise you have to make sure your special bean (and consequently its init method) will be created after all your other beans to be able to use them.
Reply With Quote
gregd
View Public Profile
Send a private message to gregd
Find all posts by gregd
#5
Old Jul 17th, 2008, 11:40 AM
brismith brismith is offline
Junior Member

Join Date: Mar 2008
Location: Taylors, SC USA
Posts: 8
Default
I'll take a look at that too. I needed to do something almost identical to what imbali described, and jfredickson's suggestion worked for me. (I'm learning too.)
Reply With Quote
brismith
View Public Profile
Send a private message to brismith
Find all posts by brismith
#6
Old Jul 29th, 2009, 01:31 PM
moraesgp moraesgp is offline
Junior Member

Join Date: Jul 2009
Location: Brazil
Posts: 2
Post
I had the same problem. I need a class to load some properties from the databases as soon as Jetty started and put those properties in a servlet attribute to be used by all jsps during the life of the application.

I got it to work by making my class implement ServletContextAware and InitializingBean. And I made the injection of DAOs from the applicattionContext.xml.

The ServletContextAware makes sure I have access to the Servlet Context, so I can put the properties in a servlet attribute like context.setAttribute("staticUrl", properties.getStaticUrl());

The InitializingBean makes sure the method is only called after all the beans are injected in the class. Is pretty much the same as init-method="startup" mentioned by jfredrickson.

I´m only a year late but as long as Google is referencing the page, people will keep coming...
Reply With Quote
moraesgp
View Public Profile
Send a private message to moraesgp
Find all posts by moraesgp
Reply

Tags
load on startup

« Previous Thread | Next Thread »

Thread Tools
Show Printable Version Show Printable Version
Display Modes
Linear Mode Linear Mode
Hybrid Mode Switch to Hybrid Mode
Threaded Mode Switch to Threaded Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Rules

Forum Jump
User Control PanelPrivate MessagesSubscriptionsWho's OnlineSearch ForumsForums Home Core Spring Projects Announcements Core Container Data Access Web Spring Web Flow Spring Security Remoting and JMS AOP (Aspect Oriented Programming) Spring Development Tools Spring Application Performance Spring Management EJB Spring Web Services Spring-LDAP Spring Batch Spring Integration Spring Dynamic Modules Spring JavaConfig Spring BlazeDS Integration Spring Roo Meta Architecture Discussion SpringSource dm Server dm Server Announcements dm Server Installation and Configuration dm Server Application Development and Deployment dm Server Tooling dm Server General SpringSource tc Server tc Server Announcements tc Server General SpringSource Cloud Foundry Cloud Foundry Announcements Cloud Foundry General Spring Related Projects Spring Rich Client Project Spring-Modules Spring Extensions Spring Extensions Spring Python Spring db4o Spring sqlj Spring Workflow Spring Actionscript Spring se-jcr Spring Surf


All times are GMT -5. The time now is 07:25 AM.

Contact Us - Spring Framework - Archive - Top

Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Friday, October 30, 2009

Java Concurrency

Look at this website for good books and articles on java concurrency

http://www.briangoetz.com/pubs.html

Re-entract lock
http://tech.puredanger.com/2008/02/12/reentrantlock-and-the-dining-philosophers/

Tuesday, October 27, 2009

Logging best practices

http://juliusdavies.ca/logging.html

Friday, September 11, 2009

How to add all libraries from a folder into classpath on Windows.

@echo off
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R ./lib %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
echo !CLASSPATH!

Reference Source: http://stackoverflow.com/questions/524081/bat-file-to-create-java-classpath/524443

Friday, August 28, 2009

Eclipse hot deployment on tomcat.

http://www.minchu.net/blog/General/8/hot-deployment-from-eclipse

Here is the content from the above link.

Hot Deployment from Eclipse · Oct 8, 12:48 PM by Krishna Prasad V

If you are used to hot deployment for Tomcat using “My Eclipse”, then you dont have to go for a commercial plugin for Eclipse to do hot deployment! Surprised…? Configure your eclipse as below and see the results:

1. Download “File Sync” plugin from http://andrei.gmxhome.de/filesync/index.html

2. Download “Sysdeo Tomcat” plugin from http://www.sysdeo.com/eclipse/tomcatplugin

3. Install both the plugins in to your eclipse>>plugin directory (If you need help in installing, see the install guide provided for the plugins in their respective homepages)

4. Configure your File Sync to synchronize the files to your application deployed in Tomcat.

5. Hot Deployment configured!! Enjoy :)

Wednesday, August 26, 2009

Maven Archetypes for different kinds of projects.

http://docs.codehaus.org/display/MAVENUSER/Archetypes+List

Monday, August 24, 2009

Agile: Vertical Slicing vs. Horizontal Slicing.

http://agilewarrior.wordpress.com/2009/07/27/horizontal-vs-vertical-slicing/

Friday, August 14, 2009

Java Constants Design

http://blog.srikanths.net/2007/04/java-constants.html

Thursday, August 13, 2009

Oracle: Query to Eliminate Duplicates.

DELETE FROM TABLE A
WHERE rowid not in
(SELECT MIN(rowid)
FROM TABLE A
GROUP BY se3_account_nbr);

Oracle: How to update using select.

UPDATE TABLE A
12 SET (A.address_1,
13 A.address_2,
14 A.address_3,
15 A.city,
16 A.state_code,
17 A.country_code,
18 A.zip,
19 A.zip_4) =
20 (SELECT ADDRESS_1, ADDRESS_2, ADDRESS_3, CITY, STATE_CODE, COUNTRY_CODE, ZIP, ZIP_4 FROM TABLE B
21 WHERE account_nbr=A.account_nbr AND ROWNUM =1)
22 WHERE EXISTS (SELECT 1 FROM TABLE B CCA WHERE CCA.account_nbr = TABLE A.account_nbr);

Thursday, July 30, 2009

Oracle: Group by only certain fields in a table.

If you need to group by only fields, here is a way to do it. Say you have a table that has columns a,b,c,d,e and your goal is to group by b,c

SELECT MAX(a), b, c FROM MY_TABLE GROUP BY b, c

oracle inserting a blob value

use to_blob function to convert a string a blob and insert it.

For ex. insert into my_table values (to_blob('1234555');

Tuesday, March 31, 2009

Webservies- Solutions to increase reliability.

Here is a good article from Oreilly.

http://www.oreillynet.com/onlamp/blog/2002/10/soap_over_jms_what_does_it_mea.html


Kevin Bedell
Soap over JMS - what does it mean and why should I care?

* listen Speech Icon

Tuesday October 15, 2002 6:23PM
by Kevin Bedell

AddThis Social Bookmark Button

Related link: http://www.oetrends.com/cgi-bin/page_display.cgi?109

I’m a big proponent of using Web Services. I’m convinced that they are just a better way of getting certain things done. In particular, I think that Web Services provide a great way of integrating two systems that are built using different technologies.

But there’s a problem. Some applications require very high
reliability for individual transactions. Soap over HTTP is limited in this type of application. The basic problem is that HTTP itself just doesn’t provide guaranteed delivery. It wasn’t designed to and it doesn’t look as if, without modification to the protocol, it ever will. (In fact,
href="http://www-106.ibm.com/developerworks/library/ws-phtt/">the
HTTPR proposal from IBM is designed to meet this challenge. Unfortunately it is still just a proposal…)

One way that companies are beginning to get over this challenge is by running SOAP over JMS. What is SOAP over JMS? There are a couple answers to this question.

The first answer lies in using JMS as a replacement for HTTP as the underlying transport for SOAP communications. Using Apache Axis, this means sending messages using the Axis API’s, but having the actual communications to the SOAP server be processed using JMS instead of being sent over HTTP. This is a great improvement in reliability for mission critical applications.

The second answer (for Axis again) is using HTTP for communications between the SOAP client and server, but having the SOAP messages be persisted in JMS inside the Web Service client before they are sent; this way they are persisted until the HTTP communications return successfully. In a different situation, it could also mean persisting the SOAP Messages using JMS inside the Web Service server application once they are received. These two features still allow for integration with other Web Service architectures, such
as .NET, but provide additional reliability by using JMS.

One approach to enhancing the reliability of Web Service
communications is to use JMS as the underlying transport directly when communicating inside your enterprise (on the ‘Intranet’), and then bridge from JMS to HTTP at the ‘edge’ of the enterprise (when going to the ‘Internet’ or an ‘Extranet’).

These features are either already a part of Axis 1.0, or are on the horizon. For more information see
href="http://www.oetrends.com/cgi-bin/page_display.cgi?109">this
article recently posted on the Open Enterprise Trends site or the Apache Axis site at http://xml.apache.org/axis .

Comments

JMS - what does it mean
Funny, your little article fail to explain just that. What is JMS and what does it mean?
Maybe I am supposed to know...
But the title sure had me fooled.
anonymous2 | October 15, 2002 11:33 PM

JMS - what does it mean
Gosh - don't I feel silly.


JMS stands for Java Messaging Service. JMS is a part of SUN's J2EE (Java 2 Enterprise Edition) standard. J2EE includes Servlets, Java ServerPages, Enterprise Java Beans (EJB's) and a number of other java enterprise techologies.


JMS provides a 'messaging' capability to J2EE - meaning that it allows you to send objects between systems or programs with assurances that the message will actually arrive.


It's convenient to think of JMS as the Java answer to IBM's MQSeries - another popular messaging technology - though some would argue with the comparison.


The point of this article is that JMS provides a more robust and reliable method of transporting SOAP messages than HTTP. JMS was designed with high reliability in mind, HTTP wasn't.


Thanks for reading! Hope this helps!


Kevin
kbedell | October 16, 2002 08:07 AM

JMS - what does it mean
http://www.oreilly.com/catalog/javmesser/
dave_chappell | October 17, 2002 02:57 PM

JMS in collaborative scenarios
HI,
I was wondering how JMS would be used in a collaborative scenario, between me and my B2B partner.

The reason why I am asking this asking is that, does it require my partner also to have a JMS queue, if not not will this work?

Regards
RAD

anonymous2 | April 13, 2003 11:18 PM

Added value WSIF with SOAP over JMS
We are planning to use either SOAP over JMS or SOAP over HTTP. Is there any added value in using WSIF in this case?
anonymous2 | August 5, 2003 05:17 AM

comment
sorry but I could not agree with your opinion that "this is a great improvement in reliability for mission critical applications" using JMS instead of HTTP. Because the specification of JMS didnt define the underlying transport, therefore JMS could use unreliable transport such UDP. In this case JMS isnt reliable at all. If you really need reliable transport you'll need to add something else such as WS-Reliability etc.
ariesofuk | January 8, 2006 10:40 AM

Wednesday, January 28, 2009

When do you need to explicity specify initial context factory?

public class WsnInitialContextFactory
extends java.lang.Object
implements javax.naming.spi.InitialContextFactory

This class is the initial JNDI context factory for WebSphere Application Server. To use this class as the initial context factory for initial JNDI contexts, set the property java.naming.factory.initial to a value of com.ibm.websphere.naming.WsnInitialContextFactory. JNDI clients running in the WebSphere runtime environment should not have to set this property because it is already set in a copy of jndi.properties contained in a WebSphere runtime jar file. To set this property from a program, you can use the Java constant, javax.naming.Context.INITIAL_CONTEXT_FACTORY, for the property name, and the constant, com.ibm.websphere.naming.PROPS.INITIAL_CONTEXT_FACTORY, for the property value.

This class is only a facade of sorts and does not contain any real implementation. It delegates to an instance of the class specified by an internal property set in the properties file, jndiprovider.properties, which is part of the WebSphere installation. Users of WebSphere do not need to set this property. However, they do need jndiprovider.properties to be in their runtime environment so that it can be found by the class loader in effect.

Friday, January 16, 2009

XA vs non XA transactions.

Here is a good write up from server side.com

http://www.theserverside.com/discussions/thread.tss?thread_id=21385

XA and NonXA datasource
Posted by: Mike Spille on Thu Sep 11 17:28:55 EDT 2003 in response to Message #95297
An XA transaction, in the most general terms, is a "global transaction" that may span multiple resources. A non-XA transaction always involves just one resource.

An XA transaction involves a coordinating transaction manager, with one or more databases (or other resources, like JMS) all involved in a single global transaction. Non-XA transactions have no transaction coordinator, and a single resource is doing all its transaction work itself (this is sometimes called local transactions).

XA transactions come from the X/Open group specification on distributed, global transactions. JTA includes the X/Open XA spec, in modified form.

Most stuff in the world is non-XA - a Servlet or EJB or plain old JDBC in a Java application talking to a single database. XA gets involved when you want to work with multiple resources - 2 or more databases, a database and a JMS connection, all of those plus maybe a JCA resource - all in a single transaction. In this scenario, you'll have an app server like Websphere or Weblogic or JBoss acting as the Transaction Manager, and your various resources (Oracle, Sybase, IBM MQ JMS, SAP, whatever) acting as transaction resources. Your code can then update/delete/publish/whatever across the many resources. When you say "commit", the results are commited across all of the resources. When you say "rollback", _everything_ is rolled back across all resources.

The Transaction Manager coordinates all of this through a protocol called Two Phase Commit (2PC). This protocol also has to be supported by the individual resources.

In terms of datasources, an XA datasource is a data source that can participate in an XA global transaction. A non-XA datasource generally can't participate in a global transaction (sort of - some people implement what's called a "last participant" optimization that can let you do this for exactly one non-XA item).

For more details - see the JTA pages on java.sun.com. Look at the XAResource and Xid interfaces in JTA. See the X/Open XA Distributed Transaction specification. Do a google source on "Java JTA XA transaction".

-Mike

Wednesday, January 7, 2009

Good technology blog to read on a daily basis to get myself updated on latest trends in software development.

http://www.labnol.org/internet/most-useful-web-applications/6278/

Tool to create screen shots of all actions on a PC.

There is a tool that can record all your actions on your PC and play it for you.

Here is the link for it:

http://www.jingproject.com/

It does more than Snag It or any other tool I used.

Friday, January 2, 2009

JMS: How to purge queues.

This is an example from below:
http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Addison-Wesley/dp/0321200683/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1195232579&sr=8-1

This book contains many examples and patterns which are wonderfully handy in messaging.

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
public class ChannelPurger extends JmsEndpoint
{
public static void main(String[] args)
{
if (args.length != 1) {
System.out.println("Usage: java ChannelPurger ");
System.exit(1);
}String queueName = new String(args[0]);
System.out.println("Purging queue " + queueName);
ChannelPurger purger = new ChannelPurger();
purger.purgeQueue(queueName);
}
private void purgeQueue(String queueName)
{
try {
initialize();
connection.start();
Queue queue = (Queue) JndiUtil.getDestination(queueName);
MessageConsumer consumer = session.createConsumer(queue);
while (consumer.receiveNoWait() != null)
System.out.print(".");
connection.stop();
} catch (Exception e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
// ignore
}
}
}
}
}