public static String getProperty(String name, String defaultValue) { String value = getProperty(name); return StringUtils.isNotEmpty(value) ? value : defaultValue; }
private static KafkaConsumer<String, byte[]> newKafkaConsumer(String group) { Properties props = new Properties(); props.put("bootstrap.servers", PropertyHolder.getProperty("message.kafka.bootstrap.servers")); // 消费者的组id props.put("group.id", group);// 这里是GroupA或者GroupB props.put("enable.auto.commit", PropertyHolder.getProperty("message.kafka.enable.auto.commit", "true")); props.put("auto.commit.interval.ms", PropertyHolder.getProperty("message.kafka.auto.commit.interval.ms", "1000")); // 从poll(拉)的回话处理时长 props.put("session.timeout.ms", PropertyHolder.getProperty("message.kafka.session.timeout.ms", "30000")); // poll的数量限制 props.put("max.poll.records", PropertyHolder.getProperty("message.kafka.max.poll.records", "100")); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.ByteArrayDeserializer"); return new KafkaConsumer<String, byte[]>(props); }
public static String getProperty(String name) { String value = PROPERTIES.get(name); if (org.apache.commons.lang3.StringUtils.isNotEmpty(value)) { int startIndex = value.indexOf("${"); if (startIndex != -1) { String key = value.substring(startIndex + 2, value.indexOf("}", startIndex + 2)); if (!org.apache.commons.lang3.StringUtils.equals(name, key) && org.apache.commons.lang3.StringUtils.isNotEmpty(key)) { String kv = getProperty(key); value = StringUtils.replace(value, "${" + key + "}", kv); } } } return value; }
public DbParam(String prefix) { this.url = PropertyHolder.getProperty(prefix + ".db.url"); Assert.notEmpty(this.url, ErrorCodeDef.DB_URL_NOT_SET); this.username = PropertyHolder.getProperty(prefix + ".db.username"); // Assert.notEmpty(this.username, ErrorCodeDef.DB_USERNAME_NOT_SET, prefix); String password = PropertyHolder.getProperty(prefix + ".db.password"); // Assert.notEmpty(password, ErrorCodeDef.DB_PASSWORD_NOT_SET, prefix); setPassword(password); init(prefix); }
public static String getProjectName() { String name = getProperty("project.name"); if (StringUtils.isEmpty(name)) { String realPath = StringUtils.replaceEach( PropertyHolder.class.getClassLoader().getResource(GlobalConstants.BLANK).getPath(), new String[] { "/target/classes/", "/WEB-INF/classes/" }, new String[] { GlobalConstants.BLANK, GlobalConstants.BLANK }); name = realPath.substring(realPath.lastIndexOf(GlobalConstants.PATH_SPLITOR)); } return name; } }
public static KafkaProducer<String, byte[]> getProducer() { synchronized (lock) { if (kafkaProducer == null) { Properties props = new Properties(); props.put("bootstrap.servers", PropertyHolder.getProperty("message.kafka.bootstrap.servers")); props.put("acks", PropertyHolder.getProperty("message.kafka.acks", "all")); props.put("retries", PropertyHolder.getIntProperty("message.kafka.retries", 0)); props.put("batch.size", PropertyHolder.getIntProperty("message.kafka.batch.size", 16384)); props.put("linger.ms", PropertyHolder.getIntProperty("message.kafka.linger.ms", 1)); props.put("buffer.memory", PropertyHolder.getLongProperty("message.kafka.buffer.memory", 33554432L)); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); kafkaProducer = new KafkaProducer<String, byte[]>(props); } } return kafkaProducer; }
private static Address[] getAddresses() { String address = PropertyHolder.getProperty(REDIS_ADDRESS); Assert.notEmpty(address, ErrorCodeDef.REDIS_ADDRESS_NOT_SET, REDIS_ADDRESS); return ProtocolUtil.parseAddress(address); }
protected Address[] getAddresses() { String address = PropertyHolder.getProperty(REDIS_ADDRESS); Assert.notEmpty(address, ErrorCodeDef.REDIS_ADDRESS_NOT_SET, REDIS_ADDRESS); return ProtocolUtil.parseAddress(address); }
public static MessagePublisher createMessagePublisher() { if (messagePublisher == null) { String messageModel = PropertyHolder.getProperty("message.model"); Assert.notEmpty(messageModel, ErrorCodeDef.MESSAGE_MODEL_NOT_SET); ServiceLoader<MessagePublisher> serviceLoader = ServiceLoader.load(MessagePublisher.class); for (MessagePublisher c : serviceLoader) { if (messageModel.equals(c.getName())) { messagePublisher = c; break; } } if (messagePublisher == null) { throw new InitializationException(ErrorCodeDef.MESSAGE_MIDDLE_NOT_FOUND); } } return messagePublisher; }
public static MessageSubcriberFactory createMessageSubcriberFactory() { if (messageSubcriberFactory == null) { String messageModel = PropertyHolder.getProperty("message.model"); Assert.notEmpty(messageModel, ErrorCodeDef.MESSAGE_MODEL_NOT_SET); ServiceLoader<MessageSubcriberFactory> serviceLoader = ServiceLoader.load(MessageSubcriberFactory.class); for (MessageSubcriberFactory c : serviceLoader) { if (messageModel.equals(c.getName())) { messageSubcriberFactory = c; break; } } if (messageSubcriberFactory == null) { throw new InitializationException(ErrorCodeDef.MESSAGE_MIDDLE_NOT_FOUND); } } return messageSubcriberFactory; } }
public static ICache getCache() { if (cache == null) { String cacheModel = PropertyHolder.getProperty("cache.model"); Assert.notEmpty(cacheModel, ErrorCodeDef.CACHE_MODEL_NOT_SET); ServiceLoader<ICache> serviceLoader = ServiceLoader.load(ICache.class); for (ICache c : serviceLoader) { if (cacheModel.equals(c.getName())) { cache = c; break; } } if (cache == null) { throw new InitializationException(ErrorCodeDef.CACHE_NOT_FOUND); } } return cache; }
private static void setHttpProxy() { // 检查并设置http代理 // if (StringUtils.isEmpty(System.getProperty("http.proxyHost"))) { String httpProxy = PropertyHolder.getProperty("http.proxy"); if (StringUtils.isEmpty(httpProxy)) { httpProxy = System.getenv("http_proxy"); } if (StringUtils.isNotEmpty(httpProxy)) { Address[] protocols = ProtocolUtil.parseAddress(httpProxy); if (CommonUtil.isNotEmpty(protocols)) { Address addr = protocols[0]; System.setProperty("http.proxyHost", addr.getHost()); System.setProperty("http.proxyPort", addr.getPort() + GlobalConstants.BLANK); String nonProxyHosts = PropertyHolder.getProperty("http.nonProxyHosts"); if (StringUtils.isNotEmpty(nonProxyHosts)) { System.setProperty("http.nonProxyHosts", nonProxyHosts); } logger.info("set http proxy[{0}:{1}] success", addr.getHost(), addr.getPort()); } } // } }
public ShardedRedisCache() { String cacheModel = PropertyHolder.getProperty("cache.model"); if (CACHE_MODEL.equals(cacheModel)) { Address[] addresses = getAddresses(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(addresses.length); for (Address addr : addresses) { shards.add(new JedisShardInfo(addr.getHost(), addr.getPort())); } shardedPool = new ShardedJedisPool(getConfig(), shards); } }
public RedisCache() { String cacheModel = PropertyHolder.getProperty("cache.model"); if (CACHE_MODEL.equals(cacheModel)) { Address[] addresses = getAddresses(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(addresses.length); for (Address addr : addresses) { shards.add(new JedisShardInfo(addr.getHost(), addr.getPort())); } jedisPool = new JedisPool(getConfig(), addresses[0].getHost(), addresses[0].getPort()); } }
/** * Description: <br> * * @author 王伟<br> * @taskId <br> * @param channel * @param data <br> */ @Override public void publish(String channel, byte[] data) { String broadcastChannels = PropertyHolder.getProperty("message.redis.broadcast.channels"); if (StringUtils.isNotEmpty(broadcastChannels) && CommonUtil.match(broadcastChannels, channel)) { RedisClientFactory.getBinaryJedisCluster().publish(channel.getBytes(), data); } else { messageQueue.push(channel, data); } }
public ClusterRedisCache() { String cacheModel = PropertyHolder.getProperty("cache.model"); if (CACHE_MODEL.equals(cacheModel)) { Address[] addresses = getAddresses(); Set<HostAndPort> readSet = new HashSet<HostAndPort>(); for (Address addr : addresses) { HostAndPort hostAndPort = new HostAndPort(addr.getHost(), addr.getPort()); readSet.add(hostAndPort); } cluster = new BinaryJedisCluster(readSet, PropertyHolder.getIntProperty("cache.redis.cluster.max.timeout", 100000)); } }
private static void setSocksProxy() { // 检查并设置http代理 // if (StringUtils.isEmpty(System.getProperty("socksProxyHost"))) { String socksProxy = PropertyHolder.getProperty("socksProxy"); if (StringUtils.isEmpty(socksProxy)) { socksProxy = System.getenv("socks_proxy"); } if (StringUtils.isNotEmpty(socksProxy)) { Address[] protocols = ProtocolUtil.parseAddress(socksProxy); if (CommonUtil.isNotEmpty(protocols)) { Address addr = protocols[0]; System.setProperty("socksProxyHost", addr.getHost()); System.setProperty("socksProxyPort", addr.getPort() + GlobalConstants.BLANK); logger.info("set socks proxy[{0}:{1}] success", addr.getHost(), addr.getPort()); } } // } } }
public static JedisPool getJedisPool() { synchronized (lock) { if (jedisPool == null) { String cacheModel = PropertyHolder.getProperty("message.model"); if (MESSAGE_MODEL.equals(cacheModel)) { Address[] addresses = getAddresses(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(addresses.length); for (Address addr : addresses) { shards.add(new JedisShardInfo(addr.getHost(), addr.getPort())); } jedisPool = new JedisPool(getConfig(), addresses[0].getHost(), addresses[0].getPort()); } } return jedisPool; } }
public static BinaryJedisCluster getBinaryJedisCluster() { synchronized (lock) { if (cluster == null) { String cacheModel = PropertyHolder.getProperty("cache.model"); if (CLUSTER_MESSAGE_MODEL.equals(cacheModel)) { Address[] addresses = getAddresses(); Set<HostAndPort> readSet = new HashSet<HostAndPort>(); for (Address addr : addresses) { HostAndPort hostAndPort = new HostAndPort(addr.getHost(), addr.getPort()); readSet.add(hostAndPort); } // cluster = new BinaryJedisCluster(readSet, // PropertyHolder.getIntProperty("message.redis.cluster.max.timeout", 100000)); cluster = new BinaryJedisCluster(readSet, PropertyHolder.getIntProperty("message.redis.cluster.max.timeout", 100000), 5, getConfig()); } } return cluster; } }
@Bean public EntityManagerFactory entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); String dbType = StringUtils.upperCase(PropertyHolder.getProperty("master.db.type", "mysql")); vendorAdapter.setDatabase(Database.valueOf(dbType)); vendorAdapter.setShowSql(PropertyHolder.getBooleanProperty("master.db.showSql", true)); vendorAdapter.setGenerateDdl(PropertyHolder.getBooleanProperty("master.db.generateDdl", false)); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.hbasesoft.**/*.entity"); factory.setDataSource(DataSourceUtil.registDataSource("master", new DbParam("master"))); factory.afterPropertiesSet(); return factory.getObject(); }