30 November 2017

Patrones de Diseño (en un año)

Se han utilizado diferentes patrones de diseño para diferentes necesidades, tal como, de arquitectura de componentes a nivel de servicios y de patrones de construcción OOP.

A continuación es la lista de los patrones de diseños usados:

- Descomposición de servicios para poder tener un buen limite de dominios (DDD, separation of concerns)

- @Domains: usado en la capa Rest donde está la implementación de POJOs

- Repository: donde está desacoplado la implementación de varios repositorios, dejando expuesta el mismo contrato a los cliente.

- @Service: capa Rest, tiene Patrón: Service-layer, en donde implementamos diferentes servicios para llamar, sean base datos pertenecientes a CDN o directamente como cliente al bus de servicios.

- @Controller: Rest, es el controller de springmvc, que es parte del patrón de diseño MVC, el cual dejamos claro el manejo del flujo del servicio rest

- Factory: todos los datasources utilizados vienen del contenedor de aplicaciones (Weblogic), y es una factoría de la cual nos abstraemos de la implementación (Oracle) y usamos la conexión entregada, la cual es reutilizada en las siguientes interacciones.

- DAO: pl-generator, librería que genera DAO automático desde la metadata de oracle y tenemos la interfaz (Façade) de los stored procedures de la base de datos, por directriz (generalmente en bancos) siempre accedemos a través de esos SP, y la librería gracias a esta encapsulación entregada por DAO podemos abstraernos de todo lo necesario de conexiones JDBC.

- SAL: Service Access Layer: Rest, patrón que viene desde la arquitectura SOA y tenemos implementada los diferentes servicios en un solo package.

- MVC: angular/rest. Usamos el patrón Model-View-Controller en nuestra solución. View es AngularJS, Controller y Model está implementado en Rest en las clases con anotaciones @Controller y Pojos, aprovechando la separación entregada por Spring

- API-Proxy: Usamos servicios Rest para todos los accesos desde front hacia OSB/DB. nunca se puede acceder directo desde Front, asi se usa api-proxy como interfaz.

- Aggregator: se utiliza en message-aggregator-rest para encapsular la implementación de del motor de envio de emails. De esta manera le damos más valor al request del envío del correo (Decorator) y tenemos un fachada (facade) para la implementación final.

- Connection Factory: JMS, todos los JMS utilizan un Connection Factory implementada por WLS. de esta manera se reutilizan las conexiones JMS.

- Prototype: en javascript para extender funciones y hacer honor al límite de 100 líneas por función (modular), se utiliza el patrón prototype.

- Database per service: notificaciones utiliza 1 schema por su solución, y no compartimos la base de datos con otros servicios, si alguien más necesita insertar datos estos deben hacerse a través un el api-proxy expuesto por notificaciones.

- Shared database (base datos compartida), existen schemas que son utilizados por varios servicios, tal como, session_cn.

- Log aggregation, se logea las transacciones y funcionalidades importantes de cada uno de los servicios en un log de aplicación que está configurado en cada uno de los dominios de WLS.

- Client-side UI composition. Cada aplicación puede tener componentes propios que están encapsulados en directivas angular. Estas directivas pueden ser reutilizadas por otras aplicaciones. El equipo de Diseño se preocupa de generar el esqueleto de las páginas, componiendo diferentes componentes UI.

- además utilizamos de varios patrones de diseño de más bajo nivel GoF: iterator, façade, singleton, Factory method pattern, Decorator, Builder, etc.


Java Performance

This is a summary post of good practices (many easy) to improve performance in your Java code:
  1. use Apache commons-lang StringUtils.replace() instead String.replace(). The big difference is because Java replace() uses regexp which makes it more expensive immediately.
  2. avoid regexp if possible (many times we use regexp and meanwhile a String.indexof() is enough).  also, Apache commons lang: StringUtils.replace("hola\nmundo\n", "\n", "");
  3. Compile once a Regexp Pattern:  Pattern pattern_dst = Pattern.compile("destinationAddress\\\"\\:\\\"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\\"");
  4. Apache commons lang, now has separated packages for regexp methods, such us, replaceall().
  5. on the same, String.split also has issues when it's not a simple character the splitter
  6. avoid using Java7 UUID (there are other faster libraries for it) (randomness trends to be slow).
    https://github.com/cowtowncoder/java-uuid-generator
    https://github.com/jchambers/fast-uuid    (* this looks interesting)
    https://github.com/codahale/fast-uuid
  7. Asynchronous Logging Parameterized Messages (log4j2) (Ring buffer - LMAX Disruptor)
    Many logging libraries offer an API for logging parameterized messages. This enables application code to look something like this:
    logger.debug("Entry number: {} is {}", i, entry[i]);
    without API:
    if (logger.isDebugEnabled()) {
        logger.debug("Entry number: " + i + " is " + entry[i].toString());
    }
    The best solution is placeholders and if{}
    if (logger.isDebugEnabled()) {
        logger.debug("Entry number: {} is {}", i, entry[i]);
    } 
    lambda java8:
    logger.trace("Some long-running operation returned {}", () -> expensiveOperation());
  8. Jasper Reports has a lazy loading at init() :
    JasperReportsContext jasperReportsContext = new SimpleJasperReportsContext();
            LocalJasperReportsContext localJasperReportsContext = new LocalJasperReportsContext(jasperReportsContext);
            localJasperReportsContext.setClassLoader(this.getClass().getClassLoader());
  9. StringBuilder or StringBuffer over String (for concatenation)
  10. Primitive over wrapper, such us, Integer, Double etc, ie:
    The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
    If you need an object instead, then use Integer.valueOf(), Long.valueOf()
  11. Sorted of sorted List Java sorted ??? (Big Theta!)
  12. String concatenating one line, it's not a problem
  13. BigInteger & BigDecimal are expensive. (avoid)
  14. Cache database connections. Object pooling. Apache common pool, Fast Object Pool, Vibur Object Pool
  15. Tune fetchSize when querying a big number of rows from DB. (default is 10, which makes it slow) 
  16. Mapper Objects: use MapStruct (fastest).
  17. log4j contention when writing to log file (async, buffer, network as a solution)
  18. exceptions are "expensive"
  19. Timeouts! (trigger'm!)
    Socket_timeouts TCP_NODELAY, SO_SNDBUF, SO_RCVBUF,  HTTP, JAX-WS, RestTemplate, JDBC timeouts :P
  20. Thread synchronized (as seen on log4j2), blocking java monitor 
  21. HTTP 1.1 instead 1.0 (keep-alive)
  22. Data source sizing
  23. PMD has several performance rules:  (among others)
    Don’t create instances of already existing BigInteger (BigInteger.ZERO, BigInteger.ONE)
    Avoid instantiating Boolean objects; you can reference Boolean.TRUE, Boolean.FALSE.
  24. Most of PMD, checkstyles rules are embedded at sonarqube.
  25. Jackson ObjectMapper must be reused (it's thread-safe). It is expensive.(use 2.8.7+ by race condition). Singleton, static or Object pooling (see above).
  26. CompletableFuture uses a forkjoin thread pool, which is a thread pool shared between all CompletableFutures and all parallel streams.
    https://dzone.com/articles/concurrency-in-action-using-javas-completable-futu
  27. https://github.com/devwebcl/async-springmvc-poc
  28. Java 8 parallel streams pitfall
    https://www.baeldung.com/java-8-parallel-streams-custom-threadpool https://medium.com/@michaelbespalov/parallel-stream-pitfalls-and-how-to-avoid-them-91f11808a16
  29. JVM CPU measure. It may be expensive for some JVM
  30. Every time you make something "static", consider if you want to make it "final" too. In most cases, you do. "static final"-s are much more optimize-able. https://t.co/Xm0YgDUTtL (from @shipilev) 
  31. Netty is a popular asynch framework, but it can suffer of OOM: io.netty.maxDirectMemory
  32. DO NOT use java 11 (~30% slower)
  33. XML parsers: SAX <= JAXB << DOM (ms).
    However jaxb es less complex than SAX for coding.
  34. XML streaming based: StAX << SAX/XOM

try-with-resources

A known, and now kind of old feature of Java 7, but still isn't as popular as we wish.

Straightforward (and easy) implementation, where you need to declare resources (File, Stream) into parenthesis of "try" keyword:

private static void printFileJava7() throws IOException {

    try(InputStream input = new FileInputStream("hola.txt")) {

        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();
        }
    }
}

And JVM will have the responsibility to close those resources.

Beware: that the object in try() must implement AutoCloseable.

More info:

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

http://tutorials.jenkov.com/java-exception-handling/try-with-resources.html

https://www.mkyong.com/java/try-with-resources-example-in-jdk-7/


24 November 2017

HTTP Codes - Arquitectura Restful

La arquitectura Rest se basa fuertemente en el protocolo HTTP, por lo cual es importante responder (y parsear al recibir) los diferentes códigos HTTP que existen.
Los códigos están agrupados por las siguientes categorías:
  • 1xx – Informational
  • 2xx – Successful
  • 3xx – Redirection
  • 4xx - Client Error
  • 5xx - Server Error

El proyecto  deberia uso granular de estos códigos con alguna libreria transversal.

Deberíamos usar solo los códigos más útiles (top “10”) que son los siguientes:

HTTP Response codes

200OKSuccessfully executed. should be used to indicate nonspecific success
201CREATEDSuccessfully executed and a new resources has been created. The response body is either empty or contains a URI(s) of the created resource. The location header should should also contain the new URI
202ACCEPTEDThe request was valid and has been accepted but has not yet been processed. The response should include a URI to poll for status updates on the request. Allows for asynchronous request ( start of an asynchronous action)
204NO_CONTENTThe request was successful but the server did not have a response. should be used when the response body is intentionally empty.
Ejemplo: Si se busca el listado de productos para un cliente y el cliente existe pero la lista es vacía, el estado es 204
301MOVED_PERMANENTLYThe new location should be returned in the response ( should be used to relocate resources)
302REDIRECTThe HTTP response status code 302 Found is a common way of performing URL redirection.
400BAD_REQUESTMalformed or invalid request
401UNAUTHORIZEDInvalid authorization credentials. (no autenticado, o autenticado incorrectamente)
403FORBIDDENDisallowed request. Security error. (no autorizado para ver pagina/recurso)
404NOT_FOUNDResource not found.
Ejemplo: Si se busca un cliente por rut y éste no existe, el estado es 404
405METHOD_NOT_ALLOWEDMethod (verb) not allowed for requested resource. Response will provide an “Allow” header to indicate what is allowed
412PRECONDITION_FAILEDOne or more conditions given in the request header fields evaluated to false when tested on the server.
415UNSUPPORTED_MEDIA_TYPEClient submitted a media type that is incompatible for the specified resource.
422UNPROCESSABLE_ENTITYSe ha producido un error funcional. El servicio reconoce los parámetros como válidos, pero alguna condición de negocio no se puede llevar a cabo. Ej: "Monto Pagado+monto capitalizado no puede ser mas que monto adeudado+monto vencido+monto adeudado ajuste-monto condonado para monto XXX" invocando a servicio bus para cálculo de pago.
418I'm a teapotLudic HTTP
500INTERNAL_SERVER_ERRORCatchall for server processing problem
503SERVICE_UNAVAILABLEResponse in the face of too many request
504GATEWAY_TIMEOUTserver error response code indicates that the server, while acting as a gateway or proxy, did not get a response in time from the upstream server that it needed in order to complete the request.

HTTP verbs:
 

  1. GET. The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. (idempotent)
  2. POST. The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server (payload)
  3. HEAD. The HEAD method asks for a response identical to that of a GET request, but without the response body.
  4. PUT. The PUT method replaces all current representations of the target resource with the request payload.
  5. DELETE. The DELETE method deletes the specified resource.
  6. CONNECT. The CONNECT method establishes a tunnel to the server identified by the target resource.
  7. OPTIONS. The OPTIONS method is used to describe the communication options for the target resource.  OPTIONS sirve para verificar CORS!
  8. TRACE. The TRACE method performs a message loop-back test along the path to the target resource.
  9. PATCH. The PATCH method is used to apply partial modifications to a resource.


more details:

  • HEAD - No defined body semantics.
  • GET - No defined body semantics.
  • DELETE - No defined body semantics.
  • CONNECT - No defined body semantics
  • PUT - Body supported.
  • POST - Body supported.
  • TRACE - Body not supported.
  • OPTIONS - Body supported but no semantics on usage (maybe in the future).


Mayor información

03 November 2017

Simple HTTP Server

python -m SimpleHTTPServer 8000

if you face :
/usr/bin/python: No module named SimpleHTTPServer

it's because for python 3:

python3 -m http.server 8000

02 November 2017

Cannot retrieve WLS app info

Cannot retrieve:

The configuration for app is still being loaded from your last request, please wait a moment and retry.

####<Oct 30, 2017 9:59:44 AM CLT> <Error> <Console> <lablnx297> <AdminServer> <[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'> <weblogic> <> <cc19eb40-e1ae-46e5-80cb-dbf2a3509811-00000f0c> <1509368384819> <BEA-240003> <Administration Console encountered the following error: java.lang.NullPointerException
  at weblogic.application.internal.ApplicationRuntimeMBeanImpl.getComponentRuntimes(ApplicationRuntimeMBeanImpl.java:449)
  at sun.reflect.GeneratedMethodAccessor1348.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at weblogic.management.jmx.modelmbean.WLSModelMBean.getAttribute(WLSModelMBean.java:525)
  at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:647)
  at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678)
  at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$12.run(WLSMBeanServerInterceptorBase.java:326)
  at java.security.AccessController.doPrivileged(Native Method)
  at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.getAttribute(WLSMBeanServerInterceptorBase.java:324)
  at weblogic.management.mbeanservers.internal.JMXContextInterceptor.getAttribute(JMXContextInterceptor.java:157)
  at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$12.run(WLSMBeanServerInterceptorBase.java:326)
  at java.security.AccessController.doPrivileged(Native Method)
  at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.getAttribute(WLSMBeanServerInterceptorBase.java:324)
  at weblogic.management.mbeanservers.internal.SecurityInterceptor.getAttribute(SecurityInterceptor.java:300)
  at weblogic.management.jmx.mbeanserver.WLSMBeanServer.getAttribute(WLSMBeanServer.java:279)
  at weblogic.management.mbeanservers.internal.JMXConnectorSubjectForwarder$5$1.run(JMXConnectorSubjectForwarder.java:327)
  at java.security.AccessController.doPrivileged(Native Method)
  at weblogic.management.mbeanservers.internal.JMXConnectorSubjectForwarder$5.run(JMXConnectorSubjectForwarder.java:325)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
  at weblogic.management.mbeanservers.internal.JMXConnectorSubjectForwarder.getAttribute(JMXConnectorSubjectForwarder.java:320)
  at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
  at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
  at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1332)
  at java.security.AccessController.doPrivileged(Native Method)
  at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1431)
  at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:661)
  at javax.management.remote.rmi.RMIConnectionImpl_WLSkel.invoke(Unknown Source)
  at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:701)
  at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:527)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:146)
  at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:523)
  at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
  at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
  at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
>

HTTP Cluster WebLogic Server

Old Deprecated feature:

web.xml :

<servlet><servlet-name>ProxyServlet</servlet-name>
<servlet-class>weblogic.servlet.proxy.HttpProxyServlet</servlet-class>
<init-param>
<param-name>redirectURL</param-name>
<param-value>http://localhost:8080</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ProxyServlet</servlet-name>
<url-pattern>/MyProducerApp/*</url-pattern>
</servlet-mapping>

.

31 October 2017

Clear Cache WLS

Before deleting any directory, you need to shutdown all servers first.

The following provides a generic example path to the tmp directory in question:

WINDOWS: 
cd C:\wls\user_projects\domains\servers\

UNIX:
cd /wls/user_projects/domains/servers/

The following would be used to clear the cache:

WINDOWS:  
rd C:\wls\user_projects\domains\servers\tmp
rd C:\wls\user_projects\domains\servers\cache


UNIX: 
rm -rf /wls/user_projects/domains/servers/tmp
rm -rf /wls/user_projects/domains//servers/cache



 For WLS versions 9.2 and above, delete the following Admin and Managed server cache directories:

    /servers/cache
   
/servers/stage (if exists)
   
/servers/tmp
 




18 October 2017

WLS JMS Configuration

Essential steps to configure a JMS environment


CORE_WLS_Domain --> Services --> Persistent Stores -->

New:

Name: FileStore-Mensajeria1
Target: Managed Server 1 (WLS01)
Directory: jmsMensajeria_1
(Activate Changes)

Name: FileStore-Mensajeria2
Target: Managed Server 2 (WLSS02)
Directory: jmsMensajeria_2
(Activate Changes)


CORE_WLS_Domain --> Services --> Messaging --> JMS Servers

New:

Name: JMSServer-Mensajeria_1
Persistent Store: FileStore-Mensajeria1
Target: WLS01 (migratable)
(finish)

Name: JMSServer-Mensajeria_2
Persistent Store: FileStore-Mensajeria2
Target: WLS02 (migratable)
(finish)

(Activate Changes)


CORE_WLS_Domain --> Services --> Messaging --> JMS Modules

Name: JMSModule_Mensajeria
Target: wls_core_cluster
(next/finish)
(Activate Changes)


entrar a JMS Module creado: (crear subdeployment)
CORE_WLS_Domain --> Services --> Messaging --> JMS Modules --> JMSModule_Mensajeria --> Subdeployments

New:

name: MensajeriaSubdeployment
Target: JMSServer-Mensajeria_1, JMSServer-Mensajeria_2
(Activate Changes)


CORE_WLS_Domain --> Services --> Messaging --> JMS Modules --> JMSModule_Mensajeria

New:

Connection Factory (next)

Name: ConnectionFactory-Mensajeria
JNDI Name: jms/ConnectionFactory-Mensajeria (next)
(finish)
(Activate Changes)

New:

Distributed Queue (next)

Name: DistributedQueue-Mensajeria
JNDI Name: jms/journal-standar (next)
Advanced Targeting: MensajeriaSubdeployment

JMS Servers: JMSServer-Mensajeria_1, JMSServer-Mensajeria_2
(finish)
(Activate Changes)



Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.