From c6715c4fa91998d9b8743e87b2468c4cf3240f0e Mon Sep 17 00:00:00 2001 From: alberto Date: Fri, 27 Sep 2019 19:45:12 +0000 Subject: [PATCH] =?UTF-8?q?Definir=20o=20banco=20(Principal/Replica)=20por?= =?UTF-8?q?=20m=C3=A9todo=20bug#15223=20dev:trevezani=20qua:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@97920 d1611594-4594-4d17-8e1d-87c2c4800839 --- pom.xml | 10 ++--- .../routing/DynamicDataSourceHolder.java | 18 +++++++++ .../routing/MultipleDataSource.java | 10 +++++ .../routing/ReadOnlyConnection.java | 12 ++++++ .../routing/ReadOnlyRouteInterceptor.java | 39 +++++++++++++++++++ .../utilerias/ApplicationProperties.java | 20 +++++++++- 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 src/com/rjconsultores/routing/DynamicDataSourceHolder.java create mode 100644 src/com/rjconsultores/routing/MultipleDataSource.java create mode 100644 src/com/rjconsultores/routing/ReadOnlyConnection.java create mode 100644 src/com/rjconsultores/routing/ReadOnlyRouteInterceptor.java diff --git a/pom.xml b/pom.xml index 61771f0a5..f736ee6aa 100644 --- a/pom.xml +++ b/pom.xml @@ -20,11 +20,6 @@ asm 3.3.1 - - org.aspectj - aspectjrt - 1.5.4 - br.com.rjconsultores brazilutils @@ -505,6 +500,11 @@ WSAG 0.0.1-SNAPSHOT + + org.aspectj + aspectjweaver + 1.8.9 + org.jboss.spec.javax.servlet jboss-servlet-api_3.0_spec diff --git a/src/com/rjconsultores/routing/DynamicDataSourceHolder.java b/src/com/rjconsultores/routing/DynamicDataSourceHolder.java new file mode 100644 index 000000000..62c806c66 --- /dev/null +++ b/src/com/rjconsultores/routing/DynamicDataSourceHolder.java @@ -0,0 +1,18 @@ +package com.rjconsultores.routing; + +public class DynamicDataSourceHolder { + private static ThreadLocal routeKey = new ThreadLocal(); + + public static String getRouteKey() { + String key = routeKey.get(); + return key; + } + + public static void setRouteKey(String key) { + routeKey.set(key); + } + + public static void removeRouteKey() { + routeKey.remove(); + } +} \ No newline at end of file diff --git a/src/com/rjconsultores/routing/MultipleDataSource.java b/src/com/rjconsultores/routing/MultipleDataSource.java new file mode 100644 index 000000000..2c98bb28d --- /dev/null +++ b/src/com/rjconsultores/routing/MultipleDataSource.java @@ -0,0 +1,10 @@ +package com.rjconsultores.routing; + +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; + +public class MultipleDataSource extends AbstractRoutingDataSource { + @Override + protected Object determineCurrentLookupKey() { + return DynamicDataSourceHolder.getRouteKey(); + } +} diff --git a/src/com/rjconsultores/routing/ReadOnlyConnection.java b/src/com/rjconsultores/routing/ReadOnlyConnection.java new file mode 100644 index 000000000..330e24621 --- /dev/null +++ b/src/com/rjconsultores/routing/ReadOnlyConnection.java @@ -0,0 +1,12 @@ +package com.rjconsultores.routing; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ReadOnlyConnection { + +} \ No newline at end of file diff --git a/src/com/rjconsultores/routing/ReadOnlyRouteInterceptor.java b/src/com/rjconsultores/routing/ReadOnlyRouteInterceptor.java new file mode 100644 index 000000000..474448602 --- /dev/null +++ b/src/com/rjconsultores/routing/ReadOnlyRouteInterceptor.java @@ -0,0 +1,39 @@ +package com.rjconsultores.routing; + +import org.apache.log4j.Logger; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties; + +@Aspect +@Component +@Order(0) +public class ReadOnlyRouteInterceptor { + private static Logger log = Logger.getLogger(ReadOnlyRouteInterceptor.class); + + @Around("@annotation(readOnlyConnection)") + public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable { + if (ApplicationProperties.getInstance().getReadOnlyConnection()) { + try { + String className = proceedingJoinPoint.getSignature().getDeclaringTypeName(); + String methodName = proceedingJoinPoint.getSignature().getName(); + + log.debug("*** READ *** [" + className + "." + methodName + "()"); + } catch (Exception e) {} + + try { + DynamicDataSourceHolder.setRouteKey("dataSourceRead"); + + return proceedingJoinPoint.proceed(); + } finally { + DynamicDataSourceHolder.removeRouteKey(); + } + } else { + return proceedingJoinPoint.proceed(); + } + } +} diff --git a/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java b/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java index 840cc48e0..240d2ceb4 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java @@ -9,9 +9,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; @@ -32,16 +32,18 @@ import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext; */ public class ApplicationProperties { - private static Logger log = Logger.getLogger(ApplicationProperties.class); private static ApplicationProperties INSTANCE; private static Properties p; private static final String PATH_IMAGENES = "/com/rjconsultores/ventaboletos/web/cliente/imagenes/"; + + private Boolean readOnlyConnection = Boolean.TRUE; private ApplicationProperties() { p = new Properties(); this.readConfiguration(); this.readConfigurationToDatabase(); + this.readReadOnlyConnection(); } public static ApplicationProperties getInstance() { @@ -200,5 +202,19 @@ public class ApplicationProperties { Application app = (Application) factory.getBean("app"); return app; } + + public void readReadOnlyConnection() { + ConstanteService constanteService = (ConstanteService) AppContext.getApplicationContext().getBean("constanteService"); + Constante constante = constanteService.buscarPorNomeConstante("READ_ONLY_CONNECTION"); + + readOnlyConnection = Boolean.TRUE; + + if ((constante != null) && StringUtils.isNotBlank(constante.getValorconstante())) { + readOnlyConnection = Boolean.valueOf(constante.getValorconstante()); + } + } + public Boolean getReadOnlyConnection() { + return readOnlyConnection; + } }