Spring 4+Redis+ActiveMQ+MongoDB+MySql+Quartz 整合 註解方式

百聯達發表於2017-04-05
一:目錄結構



二:統一配置檔案

context-datasource.properties


點選(此處)摺疊或開啟

  1. workDesk.jdbc.driverclass=com.mysql.jdbc.Driver
  2. workDesk.jdbc.url=jdbc:mysql://10.243.3.18:3306/system?userUnicode=true&characterEncoding=UTF-8
  3. workDesk.jdbc.username=root
  4. workDesk.jdbc.password=$Fortune2015
  5. workDesk.jdbc.poolsize.max=3
  6. workDesk.jdbc.poolsize.min=3
  7. workDesk.jdbc.poolsize.initial=2
  8. workDesk.jdbc.idletime.max=25000
  9. workDesk.jdbc.idleConnectionTestPeriod=18000

  10. #-------workDesk jdbc--------
  11. workDesk.read.jdbc.driverclass=com.mysql.jdbc.Driver
  12. workDesk.read.jdbc.url=jdbc:mysql://10.243.3.18:3306/system?userUnicode=true&characterEncoding=UTF-8
  13. workDesk.read.jdbc.username=root
  14. workDesk.read.jdbc.password=$Fortune2015
  15. workDesk.read.jdbc.poolsize.max=3
  16. workDesk.read.jdbc.poolsize.min=3
  17. workDesk.read.jdbc.poolsize.initial=2
  18. workDesk.read.jdbc.idletime.max=25000
  19. workDesk.read.jdbc.idleConnectionTestPeriod=18000


  20. #--------------redis conf------------
  21. redis.ip=10.243.3.18
  22. redis.port=6379
  23. redis.password=$Fortune2015
  24. redis.default.db=0
  25. redis.timeout=30000

  26. redis.pool.maxActive=1024
  27. redis.pool.maxIdle=200
  28. redis.pool.maxWait=1000
  29. redis.pool.testOnBorrow=true


  30. #------------------jms server-------------
  31. activeMq=failover:(tcp://10.243.3.18:61616,tcp://10.243.3.18:61616)?randomize=false

  32. #--------------mongodb conf------------
  33. mongodb.host=10.243.3.27
  34. mongodb.port=27017
  35. mongodb.database=gmap
  36. mongodb.username=mongodb
  37. mongodb.password=$Fortune2016
systemConfig.properties

點選(此處)摺疊或開啟

  1. #=======================================================================================

  2. #MailSender Config
  3. #=======================================================================================
  4. mail.host=smtp.mxhichina.com
三:配置檔案


點選(此處)摺疊或開啟

  1. @Configuration
  2. @EnableScheduling
  3. @EnableCaching
  4. @EnableTransactionManagement(proxyTargetClass = true)
  5. @ComponentScan("com.gemdale,com.enjoylink")
  6. @Import({DataSourceElConfig.class,TransactionManagerElConfig.class,CachingElConfig.class,MongoElConfig.class,JmsElConfig.class})
  7. public class AppConfig {

  8. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. public class CachingElConfig {

  3.     @Autowired
  4.     @Qualifier("jedisConnectionFactory")
  5.     private JedisConnectionFactory jedisConnectionFactory;

  6.     @Autowired
  7.     @Qualifier("keySerializer")
  8.     private StringRedisSerializer keySerializer;

  9.     @Autowired
  10.     @Qualifier("valueSerializer")
  11.     private JdkSerializationRedisSerializer valueSerializer;

  12.     @Bean(name = "redisTemplate")
  13.     public RedisTemplate<String, Object> redisTemplate() {
  14.         RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  15.         redisTemplate.setConnectionFactory(jedisConnectionFactory);
  16.         redisTemplate.setKeySerializer(keySerializer);
  17.         redisTemplate.setHashKeySerializer(keySerializer);
  18.         redisTemplate.setValueSerializer(valueSerializer);
  19.         redisTemplate.setHashValueSerializer(valueSerializer);
  20.         redisTemplate.afterPropertiesSet();
  21.         return redisTemplate;
  22.     }

  23.     @Bean
  24.     public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
  25.         RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
  26.         // 設定過期時間
  27.        // cacheManager.setDefaultExpiration(10);
  28.         return cacheManager;

  29.     }
  30. }



點選(此處)摺疊或開啟

  1. @Configuration
  2. @PropertySource("classpath:context-datasource.properties")
  3. public class DataSourceElConfig {

  4.     private static Logger logger = Logger.getLogger(DataSourceElConfig.class);

  5.     @Value("${workDesk.jdbc.driverclass}")
  6.     private String driverClass;
  7.     @Value("${workDesk.jdbc.url}")
  8.     private String jdbcUrl;
  9.     @Value("${workDesk.jdbc.username}")
  10.     private String user;
  11.     @Value("${workDesk.jdbc.password}")
  12.     private String password;
  13.     @Value("${workDesk.jdbc.poolsize.max}")
  14.     private int maxPoolSize;
  15.     @Value("${workDesk.jdbc.poolsize.min}")
  16.     private int minPoolSize;
  17.     @Value("${workDesk.jdbc.poolsize.initial}")
  18.     private int initialPoolSize;
  19.     @Value("${workDesk.jdbc.idletime.max}")
  20.     private int maxIdleTime;
  21.     @Value("${workDesk.jdbc.idleConnectionTestPeriod}")
  22.     private int idleConnectionTestPeriod;

  23.     @Value("${workDesk.read.jdbc.driverclass}")
  24.     private String readDriverClass;
  25.     @Value("${workDesk.read.jdbc.url}")
  26.     private String readJdbcUrl;
  27.     @Value("${workDesk.read.jdbc.username}")
  28.     private String readUser;
  29.     @Value("${workDesk.read.jdbc.password}")
  30.     private String readPassword;
  31.     @Value("${workDesk.read.jdbc.poolsize.max}")
  32.     private int readMaxPoolSize;
  33.     @Value("${workDesk.read.jdbc.poolsize.min}")
  34.     private int readMinPoolSize;
  35.     @Value("${workDesk.read.jdbc.poolsize.initial}")
  36.     private int readInitialPoolSize;
  37.     @Value("${workDesk.read.jdbc.idletime.max}")
  38.     private int readMaxIdleTime;
  39.     @Value("${workDesk.read.jdbc.idleConnectionTestPeriod}")
  40.     private int readIdleConnectionTestPeriod;

  41.     @Value("${redis.pool.maxActive}")
  42.     private int maxTotal;
  43.     @Value("${redis.pool.maxIdle}")
  44.     private int maxIdle;
  45.     @Value("${redis.pool.maxWait}")
  46.     private long maxWaitMillis;

  47.     @Value("${redis.ip}")
  48.     private String redisIp;
  49.     @Value("${redis.port}")
  50.     private int redisPort;
  51.     @Value("${redis.password}")
  52.     private String redisPassword;
  53.     @Value("${redis.timeout}")
  54.     private int redisTimeout;
  55.     @Value("${redis.default.db}")
  56.     private int redisDb;

  57.     @Value("${mongodb.host}")
  58.     private String mongoHost;
  59.     @Value("${mongodb.port}")
  60.     private int mongoPort;
  61.     @Value("${mongodb.database}")
  62.     private String mongoUserName;
  63.     @Value("${mongodb.username}")
  64.     private String mongoDB;
  65.     @Value("${mongodb.password}")
  66.     private String mongoPassword;
  67.     
  68.     @Value("${activeMq}")
  69.     private String activeMq;

  70.     @Autowired
  71.     private Environment environment;

  72.     @Bean
  73.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
  74.         return new PropertySourcesPlaceholderConfigurer();
  75.     }

  76.     /**
  77.      * MySql Master 寫庫資料來源
  78.      *
  79.      * @return
  80.      */
  81.     @Bean(name = "platformTomcat", destroyMethod = "close")
  82.     public ComboPooledDataSource platformTomcat() {
  83.         ComboPooledDataSource dataSource = new ComboPooledDataSource();
  84.         try {
  85.             dataSource.setDriverClass(driverClass);
  86.         }
  87.         catch (PropertyVetoException e) {
  88.             logger.error("資料來源配置,無法找到配置類", e);
  89.         }
  90.         dataSource.setJdbcUrl(jdbcUrl);
  91.         dataSource.setUser(user);
  92.         dataSource.setPassword(password);
  93.         dataSource.setMaxPoolSize(maxPoolSize);
  94.         dataSource.setMinPoolSize(minPoolSize);
  95.         dataSource.setInitialPoolSize(initialPoolSize);
  96.         dataSource.setMaxIdleTime(maxIdleTime);
  97.         dataSource.setTestConnectionOnCheckin(true);
  98.         dataSource.setPreferredTestQuery("SELECT 1");
  99.         dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
  100.         return dataSource;
  101.     }

  102.     /**
  103.      * MySql Slave 讀庫資料來源
  104.      *
  105.      * @return
  106.      */
  107.     @Bean(name = "platformReadTomcat", destroyMethod = "close")
  108.     public ComboPooledDataSource platformReadTomcat() {
  109.         ComboPooledDataSource dataSource = new ComboPooledDataSource();
  110.         try {
  111.             dataSource.setDriverClass(readDriverClass);
  112.         }
  113.         catch (PropertyVetoException e) {
  114.             logger.error("資料來源配置,無法找到配置類", e);
  115.         }
  116.         dataSource.setJdbcUrl(readJdbcUrl);
  117.         dataSource.setUser(readUser);
  118.         dataSource.setPassword(readPassword);
  119.         dataSource.setMaxPoolSize(readMaxPoolSize);
  120.         dataSource.setMinPoolSize(readMinPoolSize);
  121.         dataSource.setInitialPoolSize(readInitialPoolSize);
  122.         dataSource.setMaxIdleTime(readMaxIdleTime);
  123.         dataSource.setTestConnectionOnCheckin(true);
  124.         dataSource.setPreferredTestQuery("SELECT 1");
  125.         dataSource.setIdleConnectionTestPeriod(readIdleConnectionTestPeriod);
  126.         return dataSource;
  127.     }

  128.     /**
  129.      * Redis 連線池配置資訊
  130.      *
  131.      * @return
  132.      */
  133.     @Bean(name = "jedisPoolConfig")
  134.     public JedisPoolConfig jedisPoolConfig() {
  135.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  136.         jedisPoolConfig.setMaxTotal(maxTotal);
  137.         jedisPoolConfig.setMaxIdle(maxIdle);
  138.         jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
  139.         jedisPoolConfig.setTestOnBorrow(true);
  140.         return jedisPoolConfig;
  141.     }

  142.     @Bean(name = "jedisConnectionFactory")
  143.     public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
  144.         JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
  145.         jedisConnectionFactory.setUsePool(true);
  146.         jedisConnectionFactory.setHostName(redisIp);
  147.         jedisConnectionFactory.setPort(redisPort);
  148.         jedisConnectionFactory.setPassword(redisPassword);
  149.         jedisConnectionFactory.setTimeout(redisTimeout);
  150.         jedisConnectionFactory.setDatabase(redisDb);
  151.         jedisConnectionFactory.afterPropertiesSet();
  152.         return jedisConnectionFactory;
  153.     }

  154.     @Bean(name = "keySerializer")
  155.     public StringRedisSerializer keySerializer() {
  156.         return new StringRedisSerializer();
  157.     }

  158.     @Bean(name = "valueSerializer")
  159.     public JdkSerializationRedisSerializer valueSerializer() {
  160.         return new JdkSerializationRedisSerializer();
  161.     }

  162.     /**
  163.      * MongoDB 配置
  164.      *
  165.      * @return
  166.      */
  167.     @Bean(name = "mongoDbFactory")
  168.     public MongoDbFactory mongoDbFactory() throws Exception {

  169.         MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoDB,
  170.                 mongoPassword.toCharArray());
  171.         ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
  172.         MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
  173.         SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoDB);
  174.         return mongoDbFactory;
  175.     }

  176.     @Bean(name="mongoConverter")
  177.     public MongoConverter mongoConverter(MongoDbFactory mongoDbFactory) {

  178.         MappingMongoConverter mongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
  179.                 new MongoMappingContext());
  180.         // 不插入_class
  181.         mongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
  182.         return mongoConverter;
  183.     }
  184.     
  185.     
  186.     /**
  187.      * Jms 配置
  188.      * @return
  189.      */
  190.     @Bean(name="activeMQConnectionFactory")
  191.     public ActiveMQConnectionFactory activeMQConnectionFactory()
  192.     {
  193.         ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  194.         activeMQConnectionFactory.setUseAsyncSend(false);
  195.         activeMQConnectionFactory.setBrokerURL(activeMq);
  196.         return activeMQConnectionFactory;
  197.     }
  198.     
  199.     @Bean(name="cachingConnectionFactory")
  200.     public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory)
  201.     {
  202.         CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(targetConnectionFactory);
  203.         cachingConnectionFactory.setSessionCacheSize(10);
  204.         return cachingConnectionFactory;
  205.     }
  206. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. @PropertySource("classpath:context-datasource.properties")
  3. public class DataSourceElConfig {

  4.     private static Logger logger = Logger.getLogger(DataSourceElConfig.class);

  5.     @Value("${workDesk.jdbc.driverclass}")
  6.     private String driverClass;
  7.     @Value("${workDesk.jdbc.url}")
  8.     private String jdbcUrl;
  9.     @Value("${workDesk.jdbc.username}")
  10.     private String user;
  11.     @Value("${workDesk.jdbc.password}")
  12.     private String password;
  13.     @Value("${workDesk.jdbc.poolsize.max}")
  14.     private int maxPoolSize;
  15.     @Value("${workDesk.jdbc.poolsize.min}")
  16.     private int minPoolSize;
  17.     @Value("${workDesk.jdbc.poolsize.initial}")
  18.     private int initialPoolSize;
  19.     @Value("${workDesk.jdbc.idletime.max}")
  20.     private int maxIdleTime;
  21.     @Value("${workDesk.jdbc.idleConnectionTestPeriod}")
  22.     private int idleConnectionTestPeriod;

  23.     @Value("${workDesk.read.jdbc.driverclass}")
  24.     private String readDriverClass;
  25.     @Value("${workDesk.read.jdbc.url}")
  26.     private String readJdbcUrl;
  27.     @Value("${workDesk.read.jdbc.username}")
  28.     private String readUser;
  29.     @Value("${workDesk.read.jdbc.password}")
  30.     private String readPassword;
  31.     @Value("${workDesk.read.jdbc.poolsize.max}")
  32.     private int readMaxPoolSize;
  33.     @Value("${workDesk.read.jdbc.poolsize.min}")
  34.     private int readMinPoolSize;
  35.     @Value("${workDesk.read.jdbc.poolsize.initial}")
  36.     private int readInitialPoolSize;
  37.     @Value("${workDesk.read.jdbc.idletime.max}")
  38.     private int readMaxIdleTime;
  39.     @Value("${workDesk.read.jdbc.idleConnectionTestPeriod}")
  40.     private int readIdleConnectionTestPeriod;

  41.     @Value("${redis.pool.maxActive}")
  42.     private int maxTotal;
  43.     @Value("${redis.pool.maxIdle}")
  44.     private int maxIdle;
  45.     @Value("${redis.pool.maxWait}")
  46.     private long maxWaitMillis;

  47.     @Value("${redis.ip}")
  48.     private String redisIp;
  49.     @Value("${redis.port}")
  50.     private int redisPort;
  51.     @Value("${redis.password}")
  52.     private String redisPassword;
  53.     @Value("${redis.timeout}")
  54.     private int redisTimeout;
  55.     @Value("${redis.default.db}")
  56.     private int redisDb;

  57.     @Value("${mongodb.host}")
  58.     private String mongoHost;
  59.     @Value("${mongodb.port}")
  60.     private int mongoPort;
  61.     @Value("${mongodb.database}")
  62.     private String mongoUserName;
  63.     @Value("${mongodb.username}")
  64.     private String mongoDB;
  65.     @Value("${mongodb.password}")
  66.     private String mongoPassword;
  67.     
  68.     @Value("${activeMq}")
  69.     private String activeMq;

  70.     @Autowired
  71.     private Environment environment;

  72.     @Bean
  73.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
  74.         return new PropertySourcesPlaceholderConfigurer();
  75.     }

  76.     /**
  77.      * MySql Master 寫庫資料來源
  78.      *
  79.      * @return
  80.      */
  81.     @Bean(name = "platformTomcat", destroyMethod = "close")
  82.     public ComboPooledDataSource platformTomcat() {
  83.         ComboPooledDataSource dataSource = new ComboPooledDataSource();
  84.         try {
  85.             dataSource.setDriverClass(driverClass);
  86.         }
  87.         catch (PropertyVetoException e) {
  88.             logger.error("資料來源配置,無法找到配置類", e);
  89.         }
  90.         dataSource.setJdbcUrl(jdbcUrl);
  91.         dataSource.setUser(user);
  92.         dataSource.setPassword(password);
  93.         dataSource.setMaxPoolSize(maxPoolSize);
  94.         dataSource.setMinPoolSize(minPoolSize);
  95.         dataSource.setInitialPoolSize(initialPoolSize);
  96.         dataSource.setMaxIdleTime(maxIdleTime);
  97.         dataSource.setTestConnectionOnCheckin(true);
  98.         dataSource.setPreferredTestQuery("SELECT 1");
  99.         dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
  100.         return dataSource;
  101.     }

  102.     /**
  103.      * MySql Slave 讀庫資料來源
  104.      *
  105.      * @return
  106.      */
  107.     @Bean(name = "platformReadTomcat", destroyMethod = "close")
  108.     public ComboPooledDataSource platformReadTomcat() {
  109.         ComboPooledDataSource dataSource = new ComboPooledDataSource();
  110.         try {
  111.             dataSource.setDriverClass(readDriverClass);
  112.         }
  113.         catch (PropertyVetoException e) {
  114.             logger.error("資料來源配置,無法找到配置類", e);
  115.         }
  116.         dataSource.setJdbcUrl(readJdbcUrl);
  117.         dataSource.setUser(readUser);
  118.         dataSource.setPassword(readPassword);
  119.         dataSource.setMaxPoolSize(readMaxPoolSize);
  120.         dataSource.setMinPoolSize(readMinPoolSize);
  121.         dataSource.setInitialPoolSize(readInitialPoolSize);
  122.         dataSource.setMaxIdleTime(readMaxIdleTime);
  123.         dataSource.setTestConnectionOnCheckin(true);
  124.         dataSource.setPreferredTestQuery("SELECT 1");
  125.         dataSource.setIdleConnectionTestPeriod(readIdleConnectionTestPeriod);
  126.         return dataSource;
  127.     }

  128.     /**
  129.      * Redis 連線池配置資訊
  130.      *
  131.      * @return
  132.      */
  133.     @Bean(name = "jedisPoolConfig")
  134.     public JedisPoolConfig jedisPoolConfig() {
  135.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  136.         jedisPoolConfig.setMaxTotal(maxTotal);
  137.         jedisPoolConfig.setMaxIdle(maxIdle);
  138.         jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
  139.         jedisPoolConfig.setTestOnBorrow(true);
  140.         return jedisPoolConfig;
  141.     }

  142.     @Bean(name = "jedisConnectionFactory")
  143.     public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
  144.         JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
  145.         jedisConnectionFactory.setUsePool(true);
  146.         jedisConnectionFactory.setHostName(redisIp);
  147.         jedisConnectionFactory.setPort(redisPort);
  148.         jedisConnectionFactory.setPassword(redisPassword);
  149.         jedisConnectionFactory.setTimeout(redisTimeout);
  150.         jedisConnectionFactory.setDatabase(redisDb);
  151.         jedisConnectionFactory.afterPropertiesSet();
  152.         return jedisConnectionFactory;
  153.     }

  154.     @Bean(name = "keySerializer")
  155.     public StringRedisSerializer keySerializer() {
  156.         return new StringRedisSerializer();
  157.     }

  158.     @Bean(name = "valueSerializer")
  159.     public JdkSerializationRedisSerializer valueSerializer() {
  160.         return new JdkSerializationRedisSerializer();
  161.     }

  162.     /**
  163.      * MongoDB 配置
  164.      *
  165.      * @return
  166.      */
  167.     @Bean(name = "mongoDbFactory")
  168.     public MongoDbFactory mongoDbFactory() throws Exception {

  169.         MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoDB,
  170.                 mongoPassword.toCharArray());
  171.         ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
  172.         MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
  173.         SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoDB);
  174.         return mongoDbFactory;
  175.     }

  176.     @Bean(name="mongoConverter")
  177.     public MongoConverter mongoConverter(MongoDbFactory mongoDbFactory) {

  178.         MappingMongoConverter mongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
  179.                 new MongoMappingContext());
  180.         // 不插入_class
  181.         mongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
  182.         return mongoConverter;
  183.     }
  184.     
  185.     
  186.     /**
  187.      * Jms 配置
  188.      * @return
  189.      */
  190.     @Bean(name="activeMQConnectionFactory")
  191.     public ActiveMQConnectionFactory activeMQConnectionFactory()
  192.     {
  193.         ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  194.         activeMQConnectionFactory.setUseAsyncSend(false);
  195.         activeMQConnectionFactory.setBrokerURL(activeMq);
  196.         return activeMQConnectionFactory;
  197.     }
  198.     
  199.     @Bean(name="cachingConnectionFactory")
  200.     public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory)
  201.     {
  202.         CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(targetConnectionFactory);
  203.         cachingConnectionFactory.setSessionCacheSize(10);
  204.         return cachingConnectionFactory;
  205.     }
  206. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. public class JmsElConfig {

  3.     @Autowired
  4.     @Qualifier("cachingConnectionFactory")
  5.     private CachingConnectionFactory cachingConnectionFactory;

  6.     @Bean(name = "jmsTemplate")
  7.     public JmsTemplate jmsTemplate() {
  8.         JmsTemplate jmsTemplate = new JmsTemplate();
  9.         jmsTemplate.setConnectionFactory(cachingConnectionFactory);
  10.         return jmsTemplate;
  11.     }

  12.     @Bean(name = "serviceDemoDestination")
  13.     public Destination serviceDemoDestination() {
  14.         ActiveMQQueue serviceDemoQueue = new ActiveMQQueue("serviceDemoQueue");
  15.         return serviceDemoQueue;

  16.     }

  17.     @Bean(name = "jmsListenerContainerFactory")
  18.     public JmsListenerContainerFactory<DefaultMessageListenerContainer> jmsListenerContainerFactory() {
  19.         DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
  20.         jmsListenerContainerFactory.setConnectionFactory(cachingConnectionFactory);
  21.         return jmsListenerContainerFactory;
  22.     }

  23. }



點選(此處)摺疊或開啟

  1. @Configuration
  2. public class MongoElConfig {

  3.     @Autowired
  4.     @Qualifier("mongoDbFactory")
  5.     private MongoDbFactory mongoDbFactory;

  6.     @Autowired
  7.     @Qualifier("mongoConverter")
  8.     private MongoConverter mongoConverter;

  9.     @Bean(name = "mongoTemplate")
  10.     public MongoTemplate mongoTemplate() {
  11.         MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, mongoConverter);
  12.         return mongoTemplate;

  13.     }

  14. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. @PropertySource("classpath:systemConfig.properties")
  3. public class SystemElConfig {

  4.     @Value("${mail.host}")
  5.     private String mailHost;

  6.     @Autowired
  7.     private Environment environment;

  8.     @Bean
  9.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
  10.         return new PropertySourcesPlaceholderConfigurer();
  11.     }

  12.     /**
  13.      * @return the mailHost
  14.      */
  15.     public String getMailHost() {
  16.         return mailHost;
  17.     }

  18.     /**
  19.      * @param mailHost
  20.      * the mailHost to set
  21.      */
  22.     public void setMailHost(String mailHost) {
  23.         this.mailHost = mailHost;
  24.     }

  25. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. @PropertySource("classpath:systemConfig.properties")
  3. public class SystemElConfig {

  4.     @Value("${mail.host}")
  5.     private String mailHost;

  6.     @Autowired
  7.     private Environment environment;

  8.     @Bean
  9.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
  10.         return new PropertySourcesPlaceholderConfigurer();
  11.     }

  12.     /**
  13.      * @return the mailHost
  14.      */
  15.     public String getMailHost() {
  16.         return mailHost;
  17.     }

  18.     /**
  19.      * @param mailHost
  20.      * the mailHost to set
  21.      */
  22.     public void setMailHost(String mailHost) {
  23.         this.mailHost = mailHost;
  24.     }

  25. }


點選(此處)摺疊或開啟

  1. @Configuration
  2. public class TransactionManagerElConfig {

  3.     @Autowired
  4.     @Qualifier("platformTomcat")
  5.     private DataSource platformTomcat;

  6.     @Autowired
  7.     @Qualifier("platformReadTomcat")
  8.     private DataSource platformReadTomcat;

  9.     @Bean(name = "jdbcTemplate")
  10.     public JdbcTemplate jdbcTemplate() {
  11.         JdbcTemplate jdbcTemplate = new JdbcTemplate();
  12.         jdbcTemplate.setDataSource(platformTomcat);
  13.         return jdbcTemplate;
  14.     }

  15.     @Bean(name = "jdbcReadTemplate")
  16.     public JdbcTemplate jdbcReadTemplate() {
  17.         JdbcTemplate jdbcReadTemplate = new JdbcTemplate();
  18.         jdbcReadTemplate.setDataSource(platformReadTomcat);
  19.         return jdbcReadTemplate;
  20.     }

  21.     @Bean(name = "transactionManager")
  22.     public DataSourceTransactionManager transactionManager() {
  23.         DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
  24.         transactionManager.setDataSource(platformTomcat);
  25.         return transactionManager;
  26.     }

  27. }


四:Demo


點選(此處)摺疊或開啟

  1. public class App {
  2.     public static void main(String[] args) {
  3.         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

  4.         System.out.println(null == context.getBean("platformTomcat"));
  5.         System.out.println(null == context.getBean("transactionManager"));
  6.         System.out.println(null == context.getBean("redisTemplate"));
  7.         System.out.println(null == context.getBean("mongoTemplate"));
  8.         System.out.println(null == context.getBean("jmsTemplate"));

  9.         System.out.println(context.getBean(SystemElConfig.class).getMailHost());

  10.         context.getBean(ServiceDemo.class).print();

  11.     }
  12. }


點選(此處)摺疊或開啟

  1. @Service
  2. public class ServiceDemo {

  3.     @Value("#{systemElConfig.mailHost}")
  4.     private String mailHost;

  5.     public void print() {
  6.         System.out.println("=======" + mailHost);
  7.     }

  8. }


點選(此處)摺疊或開啟

  1. @Service
  2. public class ServiceDemo {

  3.     @Value("#{systemElConfig.mailHost}")
  4.     private String mailHost;

  5.     public void print() {
  6.         System.out.println("=======" + mailHost);
  7.     }

  8. }


點選(此處)摺疊或開啟

  1. @Service
  2. public class ServiceDemo {

  3.     @Value("#{systemElConfig.mailHost}")
  4.     private String mailHost;

  5.     public void print() {
  6.         System.out.println("=======" + mailHost);
  7.     }

  8. }


點選(此處)摺疊或開啟

  1. @Component
  2. @EnableJms
  3. public class ServiceDemoListener extends AbstractListener {

  4.     @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "serviceDemoQueue")
  5.     public void onMessage(Message message) {

  6.         try {
  7.             if (message instanceof ObjectMessage) {
  8.  
  9.             }
  10.         }
  11.         catch (Exception e) {
  12.             logger.error("訊息處理異常", e);
  13.         }
  14.     }

  15. }


點選(此處)摺疊或開啟

  1. @Component
  2. @EnableJms
  3. public class ServiceDemoListener extends AbstractListener {

  4.     @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "serviceDemoQueue")
  5.     public void onMessage(Message message) {

  6.         try {
  7.             if (message instanceof ObjectMessage) {
  8.  
  9.             }
  10.         }
  11.         catch (Exception e) {
  12.             logger.error("訊息處理異常", e);
  13.         }
  14.     }

  15. }


點選(此處)摺疊或開啟

  1. @Component
  2. public class ServiceDemoSchedule {

  3.     @Autowired
  4.     private GmcSmsInfoBo gmcSmsInfoBo;

  5.     @Scheduled(cron = "0 0/1 * * * ?")
  6.     public void work() {
  7.         System.out.println("========Good byte!!=======");

  8.         GmcSmsInfo smsInfo = new GmcSmsInfo();
  9.         smsInfo.setChannel("test");
  10.         smsInfo.setContent("test");
  11.         smsInfo.setStatus("01");
  12.         smsInfo.setCreateDate(Calendar.getInstance().getTime());
  13.         smsInfo.setMobile("99999999999");

  14.         try {
  15.             gmcSmsInfoBo.add(smsInfo);
  16.             
  17.             System.out.println(null==gmcSmsInfoBo.query(11386));
  18.             System.out.println(null==gmcSmsInfoBo.query(11401));
  19.         }
  20.         catch (BusinessServiceException e) {
  21.             e.printStackTrace();
  22.         }

  23.     }

  24. }


點選(此處)摺疊或開啟

  1. @Service("gmcSmsInfoBo")
  2. public class GmcSmsInfoBo extends AbstractBusinessObject {

  3.     @Autowired
  4.     private GmcSmsInfoDAO gmcSmsInfoDaoImpl;

  5.     @CachePut(value = "GmcSmsInfoCache", key = "'GmcSmsInfo_'+#result.smsId")
  6.     @Transactional(rollbackFor={Exception.class,RuntimeException.class})
  7.     public GmcSmsInfo add(GmcSmsInfo smsInfo) throws BusinessServiceException {
  8.         System.out.println("=============add==========");
  9.         try {
  10.             smsInfo.setSmsId(gmcSmsInfoDaoImpl.save(smsInfo));
  11.         }
  12.         catch (FrameworkDAOException e) {
  13.             throw new BusinessServiceException(e);
  14.         }
  15.         return smsInfo;
  16.     }

  17.     @Cacheable(value="GmcSmsInfoCache",key="'GmcSmsInfo_'+#smsId")
  18.     public GmcSmsInfo query(Integer smsId) throws BusinessServiceException {
  19.         System.out.println("=============query==========");
  20.         try {
  21.             return gmcSmsInfoDaoImpl.findById(GmcSmsInfo.class, smsId);
  22.         }
  23.         catch (Exception e) {
  24.             throw new BusinessServiceException(e);
  25.         }
  26.     }

  27. }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2136648/,如需轉載,請註明出處,否則將追究法律責任。

相關文章