Monday, November 24, 2008

Daemon Threads in Java

Any Java thread can be a daemon thread. Daemon threads are service providers for other threads running in the same process as the daemon thread.

When the only remaining threads in a process are daemon threads, the interpreter exits. This makes sense because when only daemon threads remain, there is no other thread for which a daemon thread can provide a service.

Daemon threads are sometimes called “service” threads. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. An example of a daemon thread that is continuously running is the garbage collector thread. This thread is provided by the JVM.

The Thread Object

class HelloThread extends Thread {

public void run() {

try {

for ( ; ; ) { System.out.println("hello"); sleep(1000); }

}

catch(Exception e ){}
}

}

The normal thread

public class RegularThreader{public static void main(String[] args){
Thread hello = new HelloThread();
hello.start();
thread.sleep(10000);
System.out.println("Sorry, I must be leaving destroy");
}
}

The Daemon Thread

public class DaemonThreader{

public static void main(String[] args) {

Thread hello = new HelloThread();

hello.setDaemon(true);

hello.start();
System.out.println("Sorry, I must be leaving");}}

Just the check the difference in the way ther run

1. In the first case the child thread runs continously

2. but the Daemon thread exits when the main thread is complete;

Thursday, November 20, 2008

Oracle Streams Advanced Queuing (AQ)

Advanced Queuing provides database-integrated message queuing functionality. Advanced Queuing leverages the functions of the Oracle database so that messages can be stored persistently, propagated between queues on different machines and databases, and transmitted using Oracle Net Services, HTTP(S), and SMTP.

You must set up the following data structures for certain examples to work:
CONNECT system/manager;
DROP USER aqadm CASCADE;
GRANT CONNECT, RESOURCE TO aqadm;
CREATE USER aqadm IDENTIFIED BY aqadm;
GRANT EXECUTE ON DBMS_AQADM TO aqadm;
GRANT Aq_administrator_role TO aqadm;
DROP USER aq CASCADE;
CREATE USER aq IDENTIFIED BY aq;
GRANT CONNECT, RESOURCE TO aq;
GRANT EXECUTE ON dbms_aq TO aq;
Creating a Queue Table and Queue of Object Type
CREATE OR REPLACETYPE TEST_TRANSACTION AS OBJECT(f_user_id VARCHAR2 (40),f_points NUMBER,f_update_date DATE)
commit;
BEGIN
DBMS_AQADM.CREATE_QUEUE_TABLE (
queue_table => 'TEST_TRANSACTION_TABLE',multiple_consumers => TRUE, queue_payload_type => 'TEST_TRANSACTION');
DBMS_AQADM.CREATE_QUEUE (queue_name => 'TEST_QUEUE',queue_table => 'TEST_TRANSACTION_TABLE');DBMS_AQADM.START_QUEUE (queue_name => 'TEST_QUEUE');
end;
Adding subscriber to the queue
DECLAREsubscriber SYS.AQ$_AGENT;
BEGIN
subscriber := SYS.AQ$_AGENT('SUDHEER', NULL, NULL);
SYS.DBMS_AQADM.ADD_SUBSCRIBER(queue_name => 'TEST_QUEUE',subscriber => subscriber);
END;/
commit;
Procedure to Enqueue
PROCEDURE ENQUEUE_OBJ_MSG(message IN TEST_TRANSACTION)
DECLAREmessage TEST_TRANSACTION;enqueue_options DBMS_AQ.enqueue_options_t;message_properties DBMS_AQ.message_properties_t;message_handle RAW(16);BEGINmessage := TEST_TRANSACTION('TESUSER',5,NULL);dbms_aq.enqueue(queue_name => 'TEST_QUEUE',enqueue_options => enqueue_options,message_properties => message_properties,payload => message,msgid => message_handle);
COMMIT;
END;
SELECT USER_DATA FROM AQ$TEST_TRANSACTION_TABLE
Java Agent to access Queue
public static void main(String args[]){

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("oracle.AQ.AQOracleDriver");
Connection connection = DriverManager.getConnection( "jdbc:oracle:thin:@10.1.66.109:1521:real", "ppokerops", "ppokerops");
connection.setAutoCommit(false);
AQSession aqsession = AQDriverManager.createAQSession(connection);
AQQueue queue = aqsession.getQueue("ppokerops","QUEUE1");
queue.start();
TransDAO data = null;
AQMessage message = queue.createMessage();
AQDequeueOption dq_option = new AQDequeueOption(); dq_option.setConsumerName("SUDHEER");
message = queue.dequeue(dq_option, Class.forName("com.pg.gateway.AQ.TransDAO"));
data = (TransDAO)message.getObjectPayload().getPayloadData();
connection.commit();
queue.close();
aqsession.close();
connection.close();
System.out.println("HERE"+data.getF_userid());
} catch(Exception e) {
e.printStackTrace();
}}
REFERENCE
http://download.oracle.com/docs/cd/B10500_01/appdev.920/a96587/qintro.htm
http://www.itk.ilstu.edu/docs/oracle/server.101/b10785/aq_start.htm#i634413

Thursday, November 13, 2008

Spring AOP Basics

The best atricle i have come across to start with Spring AOP
http://www.javabeat.net/articles/51-introduction-to-springs-aspect-oriented-programminga-1.html
This can be followed by Spring tutorial from www.springframework.org

Wednesday, November 12, 2008

Fetching Property Files Using Resource Bundle

Put the property Files in the classpath ,which contains key value pairs.
If the files are putin any other location thean classpath , give the path in tomcat startup.sh or classpath.sh next to the bootstrap jar listing.
Now access the files as accessing any other Bundle
ResourceBundle myResources = ResourceBundle.getBundle("propertfilename");
Then u can do a bundle.getValue() to the required key value pair.

XML -RPC

Apache XML-RPC is a Java implementation of XML-RPC, a popular protocol that uses XML over HTTP to implement remote procedure calls.
Steps
1.Client class
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/xmlrpc"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Object[] params = new Object[]{new Integer(33), new Integer(9)};
Integer result = (Integer) client.execute("Calculator.add", params);

2.Server Class
public class Calculator {
public int add(int i1, int i2) {
return i1 + i2;
}
public int subtract(int i1, int i2) {
return i1 - i2;
}
}

3. Create Propert File
Create a property file, which contains at least one property. The property name is arbitrary, and the property value is the fully qualified name of the Calculator class. For example, like that:
Calculator=org.apache.xmlrpc.demo.Calculator
The property file must be called XmlRpcServlet.properties, and it must be located in the package org.apache.xmlrpc.webserver. In other words, you would typically put it into the directory org/apache/xmlrpc/webserver and add it to your jar file.

4. Add Entry to Web.xml
<servlet>
<servlet-name> XmlRpcServlet</servlet-name>
<servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
<init-param>
<param-name>
enabledForExtensions</param-name>
<param-value>true</param-value>
<description> Sets, whether the servlet supports vendor extensions for XML-RPC. </description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc</url-pattern>
</servlet-mapping>
Reference http://ws.apache.org/xmlrpc/

XStream - Convert XMl to Java & Vice Versa

XStream is a simple library to serialize objects to XML and back again.
XStream xstream = new XStream();
Person joe = new Person("Joe", "Walnes");
String xml = xstream.toXML(joe)
The resulting XML looks like this:
<code>
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person></code>
Deserializing an object back from XML
Person newJoe = (Person)xstream.fromXML(xml);
Reference :http://xstream.codehaus.org/

Saturday, November 1, 2008

Multiple Ajax Requests - Consequtively

When Ajax requests are submitted one after the other by hitting the same URL requests
will not be submitted by property of ajax requests, since it tries to stop multiple requests
being submitted .
This can be avoided by appending Date field with milliseconds to the url.
By doing so the Ajax identifies it as a different request and request will be re-submitted.

OSWORKFLOW - Workflow Engine

OSWorkflow is fairly different from most other workflow systems available, both commercially and in the open source world. What makes OSWorkflow different is that it is extremely flexible.
OSWorkflow can be considered a "low level" workflow implementation. Situations like "loops" and "conditions" that might be represented by a graphical icon in other workflow systems must be "coded" in OSWorkflow.

Almost all of the required libraries for OSWorkflow are included in the distribution:
OSCore 2.0.1+
PropertySet 1.2+
Jakarta commons-logging
BeanShell (optional)
BSF (optional)
EJB interfaces (not neccesarily an EJB container)
XML parser (Not required for JDK 1.4)

Various concepts in oSWorkFlow are
Workflow Definition
Workflow Concepts
Common and Global Actions
Functions
Validators
Registers
Conditions
SOAP Support
Reference
http://www.opensymphony.com/osworkflow/

JNotify - Java library that allows java application to listen to file system events

JNotify works on both Windows (Windows 2000, XP, Vista) and Linux with INotify support (Kernel 2.6.14 and above).

JNotify is a java library that allow java application to listen to file system events, such as:
file created.
file modified.
file renamed.
file deleted.
This property can be used in a number of scenarios.I have used it to reload resource bundles
whenever there is a modiofication in it.
Reference:
http://jnotify.sourceforge.net/

 
Free Domain Names @ .co.nr!