Development


Sometimes you have to use the Simple Language to determine the endpoint URI in the Spring DSL.
For example suppose the input message contains the (to endpoint URI) in an attribute called “targetID”. if you used the normal definition like the following:

<route>
<camel:from uri="vm://add"/>
<camel:to uri="vm://${in.body[0].targetID}"/>
</route>

You will end up with Camel trying to create a queue called “${in.body[0].targetID}” not its value.

in order to avoid this you can use the EIP Reception List which allow you to define a set of recipients, and inside the recipient list you can use the Simple Language as the following:

<route>
<camel:from uri="vm://add"/>
<camel:recipientList>
<camel:simple>vm://${in.body[0].targetID}</camel:simple>
</camel:recipientList>
</route>

in this case the Camel creates a queue with the value of the Simple Expression.

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");

some time you need to change the author name of the files you create by eclipse rather than the system user name.

to do so:

1- open eclipse.ini file in the eclipse directory.

2- add the following line after -vmargs line

-Duser.name=your name

References:

http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg07148.html

Next Page »