Thank you for visiting! If you liked the site, please add a bookmark, else add a critical comment! Would you like to visit Greece? Traditional Greek flag This real estate site is only available in Greek! The holy mountain

Wednesday, 8 September 2010

JDeveloper 11g:Using Log4j in stateful EJB

Since the use of log4j has proven to be a very popular subject for the readers, another code snippet for your Model project will be  given as an example. In case you prefer ADF business components, you can find an example too. If you 'd also like an example for a backing bean for your ViewController project this time, you  can have it here
First, one has to double click the project, so that the window of the libraries appears, in order to add the Log4j jar library. 
Second, navigate to C:\...\Model\src to create the log4j.properties file:

# **Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, A
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


Finally, if one 'd like to use log4j within a  stateful EJB class, the source code is similar:

...
import org.apache.log4j.Logger;

@Stateful(name = "PlaceOrder", mappedName = "ejb3inaction-Model-PlaceOrder")
@Remote
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)  
public class PlaceOrderBean implements PlaceOrder {
    Logger logger =Logger.getLogger(this.getClass().getSimpleName());
...
    @Remove                                                          
    @TransactionAttribute(TransactionAttributeType.REQUIRED)         
    public Long confirmOrder() {
      ...
      em.persist(order);            
      if (logger.isDebugEnabled())
         logger.debug("******************Order confirmed!**************");
      return order.getOrderId();
    }
...
}
Thus, one gets the following log output from the integrated Weblogic server:

Run startup time: 9656 ms.
[Application ejb3inaction deployed to Server Instance IntegratedWebLogicServer]
<2010-09-08 16:00:18.4--ServerSession(1576505)--EclipseLink, version: Eclipse Persistence Services - 2.0.2.v20100323-r6872>
<2010-09-08 16:00:18.416--ServerSession(1576505)--Server: WebLogic Server 10.3.3.0  Fri Apr 9 00:05:28 PDT 2010 1321401 >
<2010-09-08 16:00:18.837--ServerSession(1576505)--file:/C:/Users/Nick/AppData/Roaming/JDeveloper/system11.1.1.3.37.56.60/o.j2ee/drs/ejb3inaction/ModelEJB.jar/_Model login successful>
0    [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO  PlaceOrder_i7ibfi_Impl  - ******************Order confirmed!**************


In case you prefer working with Eclipse, the process is quite similar. See for example:
http://snippets.dzone.com/posts/show/3248
Finally, if you need more tips about appenders:
http://www.mobilefish.com/developer/log4j/log4j_quickguide_appenders.html

https://logging.apache.org/log4j/2.x/ 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.