<build>
<finalName>pay</finalName>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>3.0.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<url>scp://root:123456@192.168.1.174/root/service_${project.build.finalName}/webapps</url>
<fromFile>target/${project.build.finalName}.war</fromFile>
<toFile>pay.war</toFile>
<commands>
<command>...</command>
</commands>
</configuration>
</plugin>
</plugins>
</build>
mvn clean install wagon:upload-single
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.39</version>
</dependency>
public class OutputT<T> {
private int code; //返回码
private String msg; //消息
private T data; //数据
//get/set method
}
public class User {
private Long id;
private String username;
//get/set method
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class Demo {
public static void main(String[] args) {
String jsonStr = "{\"code\":0,\"msg\":\"ok\",\"data\":{\"id\":1,\"username\":\"tom\"}}";
OutputT<User> output = JSON.parseObject(jsonStr, new TypeReference<OutputT<User>>(){});
System.out.println(output);
}
}
http://undertow.io/
Undertow is extremely lightweight, with the Undertow core jar coming in at under 1Mb. It is lightweight at runtime too, with a simple embedded server using less than 4Mb of heap space.
Support for HTTP upgrade, to allow multiple protocols to be multiplexed over the HTTP port.
Undertow provides full support for Web Sockets, including JSR-356 support.
Undertow provides support for Servlet 3.1, including support for embedded servlet. It is also possible to mix both Servlets and native undertow non-blocking handlers in the same deployment.
Undertow can be embedded in an application or run standalone with just a few lines of code.
An Undertow server is configured by chaining handlers together. It is possible to add as much or as little functionality as you need, so you don’t pay for what you are not using.
maven dependency:
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.4.12.Final</version>
</dependency>
java code(Async IO):
package com.mszhe;
import io.undertow.Undertow;
import io.undertow.util.Headers;
public class Demo {
public static void main(String[] args) {
Undertow undertow = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(exchange -> {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Hello World");
}).build();
undertow.start();
}
}
maven dependency:
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.4.12.Final</version>
</dependency>
main method:
package com.mszhe;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo;
import javax.servlet.ServletException;
public class Demo2 {
public static void main(String[] args) {
ServletInfo servletInfo = Servlets.servlet("hi", MyServlet.class);
servletInfo.addInitParam("message", "hello world!");
servletInfo.addMapping("/hi");
DeploymentInfo deploymentInfo = Servlets.deployment();
deploymentInfo.setClassLoader(Demo2.class.getClassLoader());
deploymentInfo.setContextPath("/");
deploymentInfo.setDeploymentName("hi.war");
deploymentInfo.addServlets(servletInfo);
ServletContainer container = Servlets.defaultContainer();
DeploymentManager manager = container.addDeployment(deploymentInfo);
manager.deploy();
PathHandler pathHandler = Handlers.path();
HttpHandler myApp;
try {
myApp = manager.start();
} catch (ServletException e) {
throw new RuntimeException("failed!");
}
pathHandler.addPrefixPath("/", myApp);
Undertow server = Undertow.builder().
addHttpListener(8080, "localhost")
.setHandler(pathHandler).build();
server.start();
}
}
MyServlet.java:
package com.mszhe;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("<p style='color:red;text-align:center;'>"+this.getInitParameter("message")+"</p>");
writer.close();
}
}
package main
import (
"errors"
"log"
"fmt"
)
func main() {
defer func() {
if err := recover(); err != nil {
switch x := err.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("Unknow panic ")
}
log.Fatal(err)
}
}()
panic(1)
fmt.Println(12) // not executed
}
package main
import (
"fmt"
"io/ioutil"
"encoding/base64"
)
func main() {
bytes, err := ioutil.ReadFile("/file/path/to/encode")
if err != nil {
panic(err)
}
str := string(base64.StdEncoding.EncodeToString(bytes))
fmt.Println(str)
}
Integer a = 10; 相当于 Integer b = Integer.valueOf(10);package com.mszhe;
public class Demo {
public static void main(String[] args) {
Integer a = 1;
Integer b = 1;
Integer c = 512;
Integer d = 512;
System.out.println(a == b); // true
System.out.println(c == d); // false
}
}
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
-XX:AutoBoxCacheMax=NEWVALUE-Djava.lang.Integer.IntegerCache.high=NEWVALUE