Tagged: java

纯JAVA ID序列生成

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;

public enum IdGenerator {

	INSTANCE;

	private long workerId;   			//用ip地址最后几个字节标示
	private long datacenterId = 0L; 	//可配置在properties中,启动时加载,此处默认先写成0
	private long sequence = 0L;
	private long workerIdBits = 8L; 	//节点ID长度
	private long sequenceBits = 12L; 	//序列号12位
	private long workerIdShift = sequenceBits; //机器节点左移12位
	private long datacenterIdShift = sequenceBits + workerIdBits; //数据中心节点左移14位
	private long sequenceMask = ~(-1L << sequenceBits); //4095
	private long lastTimestamp = -1L;

	IdGenerator() {
		workerId = 0x000000FF & getLastIP();
	}

	public synchronized String nextId() {
		long timestamp = timeGen(); //获取当前毫秒数
		//如果服务器时间有问题(时钟后退) 报错。
		if (timestamp < lastTimestamp) {
			throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
		}
		//如果上次生成时间和当前时间相同,在同一毫秒内
		if (lastTimestamp == timestamp) {
			//sequence自增,因为sequence只有12bit,所以和sequenceMask相与一下,去掉高位
			sequence = (sequence + 1) & sequenceMask;
			//判断是否溢出,也就是每毫秒内超过4095,当为4096时,与sequenceMask相与,sequence就等于0
			if (sequence == 0) {
				timestamp = tilNextMillis(lastTimestamp); //自旋等待到下一毫秒
			}
		} else {
			sequence = 0L; //如果和上次生成时间不同,重置sequence,就是下一毫秒开始,sequence计数重新从0开始累加
		}
		lastTimestamp = timestamp;

		long suffix = (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;

		SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHMMssSSS");
		String datePrefix = sd.format(timestamp);

		return datePrefix + suffix;
	}

	protected long tilNextMillis(long lastTimestamp) {
		long timestamp = timeGen();
		while (timestamp <= lastTimestamp) {
			timestamp = timeGen();
		}
		return timestamp;
	}

	protected long timeGen() {
		return System.currentTimeMillis();
	}

	private byte getLastIP() {
		byte lastip = 0;
		try {
			InetAddress ip = InetAddress.getLocalHost();
			byte[] ipByte = ip.getAddress();
			lastip = ipByte[ipByte.length - 1];
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return lastip;
	}
}

测试

import org.junit.Assert;
import org.junit.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class IdGeneratorTest {

	@Test
	public void nextId() throws Exception {
		final IdGenerator idg = IdGenerator.INSTANCE;
		ExecutorService es = Executors.newFixedThreadPool(10);
		final HashSet idSet = new HashSet<String>();
		Collection collection = Collections.synchronizedCollection(idSet);
		long start = System.currentTimeMillis();
		System.out.println("***** start generate id ******");
		for (int i = 0; i < 10; i++)
			es.execute(() -> {
				for (int j = 0; j < 100000; j++) {
					String id = idg.nextId();
					synchronized (idSet) {
						idSet.add(id);
					}
				}
			});
		es.shutdown();
		es.awaitTermination(10, TimeUnit.SECONDS);
		long end = System.currentTimeMillis();
		System.out.println("***** end generate id *****");
		System.out.println("***** cost " + (end - start) + " ms!");
		Assert.assertEquals(10 * 100000, idSet.size());
		//idSet.forEach(o -> System.out.println(o));
	}
}

经测试,在1000000次的生成中未出现重复!!

Spring 静态资源过滤

如果web.xml配置的拦截器如下

<!-- 配置 SpringMVC DispatcherServlet -->
<servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
    <!--<multipart-config>-->
    <!--<location>/</location>-->
    <!--<max-file-size>2097152</max-file-size>-->
    <!--<max-request-size>4194304</max-request-size>-->
    <!--</multipart-config>-->
</servlet>
<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 默认匹配所有的请求 -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

那么,静态资源如js、css、图片等都会经过org.springframework.web.servlet.DispatcherServlet过滤,DispatcherServlet当然处理不了这些文件,所以这些文件就发送不到客户端了。

这样我们可以使用spring的静态资源处理器,相当于告诉SpringMVC,凡是请求路径为/assets/开始的,都自动映射到/resources目录下面对应的文件上去。

<mvc:resources mapping="/assets/**" location="/resources/"/>

同时还有一个地雷,若你配置了拦截器,且拦截的是 /** ,那么你就必须换个处理方式了。

1、使用拦截器的排除 <mvc:exclude-mapping path="你的静态资源路径" />

2、交由web容器处理,在org.springframework.web.servlet.DispatcherServlet之前加入如下了内容

<servlet-mapping>    
    <servlet-name>default</servlet-name>    
    <url-pattern>/js/*</url-pattern>    
    <url-pattern>/css/*</url-pattern>    
    <url-pattern>/images/*</url-pattern>    
    <url-pattern>/fonts/*</url-pattern>    
</servlet-mapping>

内容根据情况更改!

当然,我们还可直接让上游来直接处理静态资源,这时我们只需要在springmvc配置中加入

<mvc:default-servlet-handler/>

然后再到web.xml里面做映射即可,映射内容如下

<servlet-mapping>    
    <servlet-name>default</servlet-name>    
    <url-pattern>/js/*</url-pattern>    
    <url-pattern>/css/*</url-pattern>    
    <url-pattern>/images/*</url-pattern>    
    <url-pattern>/fonts/*</url-pattern>    
</servlet-mapping>

IDEA初始化配置

使用JetBrains IDEA已经两个月了,今天给我自动升级了,但是一直报一个错误(大致就是配置无法保存)

然后就是各种折腾~

一、修改默认配置存储路径

# /bin/idea.properties

default.path = D:/Program Files/JetBrains/idea

idea.config.path=${default.path}/.IntelliJIdea/config

#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to IDE system folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.system.path=${default.path}/.IntelliJIdea/system

#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to user installed plugins folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.plugins.path=${default.path}/plugins

#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to IDE logs folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.log.path=${default.path}/log

二、修改默认使用内存及编码

# /bin/idea64.exe.vmoptions

-Xms256m
-Xmx2048m
-Xmn512m
-XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=240m
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-ea
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-Djsse.enableSNIExtension=false
-XX:+UseCodeCacheFlushing
-XX:+HeapDumpOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-XX:MaxJavaStackTraceDepth=-1
-agentlib:yjpagent64=probe_disable=*,disablealloc,disabletracing,onlylocal,disableexceptiontelemetry,delay=10000,sessionname=IntelliJIdea2017.2
-Dfile.encoding=UTF-8

三、设置项目默认配置

default maven

Maven

Default Project Structure

Default Project Structure

Code Completion

Code Completion

Spring MVC 启动后执行方法

以前在Servlet里面我们都是重写httpServlet的init方法来达到,在Spring里面我们只需要配置一下就能达到目的

<!--注意:lazy-init="false" init-method="init" -->
<bean id="cacheLoder" lazy-init="false" class="CacheLoder" init-method="init" />

获取Spring Bean的几种方式

  1. 在初始化时保存ApplicationContext对象
  2. 通过spring提供的utils类获取ApplicationContext对象
  3. 继承自抽象类ApplicationObjectSupport
  4. 继承自抽象类WebApplicationObjectSupport
  5. 实现接口ApplicationContextAware
  6. 通过Spring提供的ContextLoader

方法1.

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 
ac.getBean("beanId");

方法2.通过Spring提供的工具类获取ApplicationContext对象

说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); 
ac1.getBean("beanId"); 
ac2.getBean("beanId");

方法3.继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

方法4.继承自抽象类WebApplicationObjectSupport

说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

方法5.实现接口ApplicationContextAware

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。

package com.pay.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring Bean工具类
 *
 * @author Jalena
 * @create 2017-05-08 23:25
 */
@Component
public class BeanFactoryUtil implements ApplicationContextAware {

	public BeanFactoryUtil() {
	}

	// spring 上下文对象
	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}


	/**
	 * 根据类唯一标识名获取Bean
	 * @param beanName 唯一标识名称(ID)
	 * @return Object
	 * @throws BeansException
	 */
	public static Object getBeanByName(String beanName) throws BeansException{
		return applicationContext.getBean(beanName);
	}

	/**
	 * 根据类Class获取Bean
	 * @param requiredType 类Class
	 * @param <T> 类型
	 * @return
	 * @throws BeansException
	 */
	public static <T> T getBeanByClass(Class<T> requiredType) throws BeansException{
		return applicationContext.getBean(requiredType);
	}

	/**
	 * 根据类唯一标识名及类Class获取Bean
	 * @param beanName 唯一标识名
	 * @param requiredType 类Class
	 * @param <T> 类型
	 * @return
	 * @throws BeansException
	 */
	public static <T> T getBeanByNameAndClass(String beanName, Class<T> requiredType) throws BeansException {
		return applicationContext.getBean(beanName, requiredType);
	}
}

方法6.通过Spring提供的ContextLoader

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);