Tagged: java

WebService 测试

引入Maven包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>Jalena</groupId>
	<artifactId>WebServiceTest</artifactId>
	<version>1.0-SNAPSHOT</version>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>

		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis-jaxrpc</artifactId>
			<version>1.4</version>
		</dependency>

		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.3</version>
		</dependency>

		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.37</version>
		</dependency>

		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.5</version>
			<exclusions>
				<exclusion>
					<artifactId>commons-logging</artifactId>
					<groupId>commons-logging</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.9.1</version>
		</dependency>


	</dependencies>

</project>

创建接口调用实例

测试类

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xjklAxiService.XjklAxiServiceServiceLocator;
import xjklAxiService.XjklAxiService_PortType;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;

public class XjklAxiServiceServiceTest {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	private XjklAxiService_PortType service;

	@Before
	public void init(){
		XjklAxiServiceServiceLocator locator = new XjklAxiServiceServiceLocator();
		try {
			service = locator.getxjklAxiService();
		} catch (ServiceException e) {
			logger.warn(e.getMessage());
		}
	}

	@Test
	public void testQry(){
		try {
			String result = service.YJstockviewQry("##2018-03-01#2018-03-15#");
			logger.debug(result);
		} catch (RemoteException e) {
			logger.warn(e.getMessage());
		}
	}
}

Log4j2 配置

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="off" status="INFO"> <!-- 这个status是控制系统信息的输出级别 -->
	<Properties>
		<Property name="path">${log4j:configParentLocation}/../../logs</Property>
		<Property name="pattern" value="%d{DEFAULT} [%-5level] %c{1.}.%M()/%L - %msg%xEx%n"/>
	</Properties>

	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">	<!-- 将日志信息从控制台输出 -->
			<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
			<PatternLayout pattern="%highlight{${pattern}}" />
		</Console>

		<File name="debug" fileName="${path}/log.log" append="true">	<!-- 将日志信息写入日志文件 -->
			<Filters>
				<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
				<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
			</Filters>
			<PatternLayout pattern="${pattern}" />
		</File>

		<RollingFile name="warn" fileName="${path}/warn.log" filePattern="${path}/warn-%d{yyyy-MM-dd}_%i.log">
			<PatternLayout pattern="${pattern}" />
			<SizeBasedTriggeringPolicy size="50MB" />
			<Filters>
				<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
				<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
			</Filters>
		</RollingFile>
	</Appenders>

	<Loggers>
		<Logger name="TestWebService" level="TRACE"/>
		<Root level="debug">
			<AppenderRef ref="Console" />   <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会在控制台输出 -->
			<AppenderRef ref="warn" />
			<AppenderRef ref="debug" />  <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会写入文件,但会创建文件 -->
		</Root>
	</Loggers>

</configuration>

纯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" />