In Java you have Many tools to schedule job to be executed in exact time in the future, here is a list of tools I have learned and used:

  • Open Symphony Quartz
  • EJB timer service
  • Java built-in Timer Task

If your are working in a web application that supports EJB 3.0 I strongly recommend using EJB timer service for the following:

  • Transaction support.
  • Persistency.
  • Repetitive executions.
  • No need for 3rd party implementations- the service provided by the EJB container
  • Ease of use.
  • Rich interfaces.

If your are working in standalone application with no container support I recommend Quartz for the following:

  • Using separate schema with support for many RBMS such mysql, oracle …etc.
  • Persistency.
  • Repetitive executions.
  • Rich interfaces.

Regarding using the Timer Task I’m not recommend using this utility at all in large scale web applications for the following:

  • Does not provide any persistency, so simply if your application terminated for any reason, all your task are gone.
  • It is not transactional.
  • This class does not offer real-time guarantees.
  • Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.
  • Timer Task has a know low performance as it creates many threads to schedule your tasks.
  • If you schedule a task the current bean or caller class will still continue running in the memory because the created thread.

Some times you need to send an email from your java application through a SMTP server. here is how you can do it simply using JavaMail API:

1- Instantiate  properties object, that contains the host name and authentication.

2- Instantiate Java Mail Session.

3- Create and fill you message.

4- Get Java Mail Transporter.

5- Connect the Transporter.

6- Send the message by the Transporter.

and here is the code:

/* create properties object */

Properties properties = new Properties();
properties.setProperty(“mail.smtp.host”, “smtp.mailserver.com”);
properties.setProperty(“mail.smtp.auth”, “true”);

/* get session */

Session session = Session.getDefaultInstance(properties, null);

/* create and fill the message */

MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(“sender@company.com”));
message.addRecipient(RecipientType.TO, new InternetAddress(“user@company.com”));
message.addRecipient(RecipientType.CC, new InternetAddress(“user@mail.com”));
message.setSubject(“testing java mail again and again!!!”);
message.setText(“Salam there, \nThis is a test from java mail”);

/* get transport */

Transport transport = session.getTransport(“smtp”);

/* connect to smtp server */
transport.connect(“smtp.mailserver.com”, “username”, “password”);

/* send the message to the recipients */
transport.sendMessage(message, message.getAllRecipients());
System.out.println(“message sent successfuly”);
} catch (AddressException e) {

e.printStackTrace();
} catch (MessagingException e) {

e.printStackTrace();
}

and enjoy sending mails from you application

when i tried to manage DI JAX-WS web services by configuring them as beans in Spring application context xml file like this:

<bean class=”org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter” />

<bean id=”partyService” class=”com.nilesys.ocean.services.party.PartyServiceImpl” >
<property name=”partyDAO” ref=”partyDAO”/>
</bean>

I got this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter#0’ defined in ServletContext resource [/WEB-INF/spring-conf/services.xml]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: NOT SUPPORTED

after 4 hours of googing  and searching i figured out that adding lazy-init=”true” to the SimpleJaxWsServiceExporter bean will solve the problem like this:

<bean class=”org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter” lazy-init=”true” />

<bean id=”partyService” class=”com.nilesys.ocean.services.party.PartyServiceImpl” >
<property name=”partyDAO” ref=”partyDAO”/>
</bean>

i wish this could help you as i didn’t found a solution for this exact problem by googling it.

sometimes you need to configure the log4j to daily roll the log file, this will help when you trace an error happened in a specific day.

in your log4j.properties file create an appender of type DailyRollingFileAppender:

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

define the date pattern which will be used in naming the log files:

log4j.appender.R.DatePattern=’-‘yyyy-MM-dd’.log’

define the path of your log files:

log4j.appender.R.File=log/application

and configure how logs should appear inside the file:

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n
log4j.rootLogger=debug, stdout, R

now you will have a separate log file for every day and will be in path

log/application-yyyy-MM-dd.log

To set HTTP header in a SOAP message :

SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();

SOAPConnection soapConnection =factory.createConnection();

SOAPMessage message = MessageFactory.newInstance().createMessage(null,yourInputStream);

message.getMimeHeaders().addHeader("header name", "header value");

soapConnection.call(message, "server url");

In this post will explain how to create a CORBA Java server using the Java sdk.

here a pref. description of what needed to be done:

1- create an idl (Interface Description Language) file that will be the contract between the Server and client regarding the object that needed to be referenced from the client and implemented on the server.

2- generate java skeleton from the idl by using idltojava compiler or idlj.

3- create the concrete class that will be instantiated by the server and its objects referenced by the client.

4- instantiate an ORB object to handle all Marshalling and un-Marshalling for the object calls and all IIOP stuff.

5- create naming context object.

6- create naming component for the object that will be exposed by CORBA.

7- bind the object with the name component in the name context.

the following the a sample code for the above steps:

1- create an IDL file: create a file and name it helloWorld.idl and add the following lines into it:

——————————————————-

module helloWorld {
interface Greetings {
string sayHello();
};
};

———————————————————————

in this idl we define one interface called Greetings that interface has one method or operation called sayHello and will return a String.

2- generate server skeleton from the idl by idlj:

in your terminal go to the directory contains the idl file and run the following command

$ idlj  -fserver -oldImplBase helloWorld.idl

the idlj will create the following:

– Greetings interface.

– GreetingsOperations class.

– _GreetingsImplBase class.

3- create a concrete class contains your business:

you need now to create the concrete class which you need its objects to be exposed through CORBA. To do so you need to create GreetingsServer class that extend the _GreetingsImplBase class.

by extending _GreetingsImplBase class you need to implement the sayHello method, here is code sample:

——————————————————————————————–

public class GreetingsServer extends _GreetingsImplBase {

public String sayHello () {

return ” Hello CORBA”;

}

}

———————————————————————————————–

save this class and create a new class the will run as the server for IIOP calls name the new class CorbaServer

————————————————————————————————–

package Corba;

import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

public class CorbaServer {

/**
* @param args
*/
public static void main(String[] args) {
// 4- create ORB object
ORB orb = ORB.init(args, null);

// create an instance of the GreetingsServer , this instance will be exposed by the ORB

GreetingsServer greetings = new GreetingsServer();
// register the instance to ORB object
orb.connect(greetings);

try {
// create naming context root, the NameService is the root NameService for any ORB
Object initNameService = orb.resolve_initial_references(“NameService”);
// narrowing the naming service object
NamingContext ncRef = NamingContextHelper.narrow(initNameService);
// create a name component for the GreetingsServer object
NameComponent nc = new NameComponent(“Greetings”,””);
// create name component array that will hold the greeting name component

NameComponent[] path = {nc};

// bind the greetings object with name component
ncRef.rebind(path, greetings);
// to keep the application waiting for calls

Thread.currentThread().join();

} catch (InvalidName e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotFound e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotProceed e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
————————————————————————————-

now you have the CORBA server we need to run the naming service that will be used by the CORBA server

$ tnameserv

this will start the naming service

at last you can start your CORBA server name and wait for calls from corba clients that used your idl.

references:

http://members.tripod.com/gsraj/corba/chapter/javaidl_1.html — this was very helpful to me.

http://java.sun.com/j2se/1.4.2/docs/guide/corba/index.html