php 不依赖第三方服务与 java 后台服务通信

服务端 java 监听端口

package com.st.xk;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class XKServer {

	private static boolean isDo = true;

	public static void main(String[] args) {
		try {
			ServerSocket ss = new ServerSocket(8090, 10, InetAddress.getByName("172.16.2.8"));
			while (isDo) {
				Socket s = ss.accept();
				new WorkerThread(s).start();
			}
			ss.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Runtime.getRuntime().addShutdownHook(new Thread(){
			public void run(){
				isDo = false;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
	}

}
package com.st.xk;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class WorkerThread extends Thread {

	private Socket socket;

	public WorkerThread(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			
			StringBuilder sb = new StringBuilder();
			String str;
			while((str = reader.readLine()) != null){
				sb.append(str);
			}
			socket.shutdownInput();
			
			writer.write(sb.toString());
			writer.flush();
			socket.shutdownOutput();
			
			reader.close();
			writer.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

客户端 php 利用 socket 连接向 java 程序发送消息

< ?php
function testSocket($i){
	$str = '{id:'.$i.',time:'.time().'}';
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	socket_connect($socket, '172.16.2.8', 8090);
	socket_write($socket, $str, strlen($str));
	socket_shutdown($socket, 1);
	while($rStr = socket_read($socket, 4096)){
		echo $rStr;
	}
	echo "\n";
	socket_close($socket);
}
?>

在非WEB环境中关闭 java 进程 及 spring 容器

static AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:context.xml");
static boolean isDo = true;

public static void main(String[] args) throws InterruptedException {
	for(int i=0; i<10; i++){
		new Producer().start();
	}
	ctx.registerShutdownHook();
	Runtime.getRuntime().addShutdownHook(new Thread() {
		public void run() {
			try {
				isDo = false;
				Thread.sleep(1000);
				//ctx.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(ctx.getBean("messageListener", TestAmqSpringMessageListener.class).getCounter());
		}
	});
}

spring java mail freemarker 发送邮件

<!-- 邮件发送器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${javax.mail.host}" />
    <property name="username" value="${javax.mail.username}" />
    <property name="password" value="${javax.mail.password}" />
    <property name="protocol" value="${javax.mail.protocol}" />
    <property name="defaultEncoding" value="${javax.mail.defaultEncoding}" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">${javax.mail.smtp.auth}</prop>
            <prop key="mail.smtp.starttls.enable">${javax.mail.smtp.starttls.enable}</prop>
        </props>
    </property>
</bean>

<!-- freeMarkerConfigurer -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/tpl/" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

public void sendMimeMessage(String from, String to, String subject, String text, File... files){
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper;
    try {
        mimeMessageHelper = new MimeMessageHelper(mimeMessage, true , "utf-8");
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(text, true);
        for(int i=0;i<files.length;i++){
            mimeMessageHelper.addAttachment(files[i].getName(), files[i]);
        }
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

nginx 负载均衡 tomcat 集群 memcached session 复制(共享)

准备工作:
将以下 jar 复制到 tomcat 目录下的 lib 目录下:
memcached-session-manager-1.6.1.jar
memcached-session-manager-tc7-1.6.1.jar
msm-javolution-serializer-1.6.1.jar
spymemcached-2.8.1.jar
javolution-5.4.3.1.jar

//********************************* nginx.conf
upstream jsp_server {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
ip_hash;
}
server {
listen 80;
server_name s.myechinese.com cas.myechinese.com;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass http://jsp_server;
}
}
//********************************* context.xml
< Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" sticky="false" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" />
//********************************* server.xml
在 Engine 节点添加 jvmRoute 属性
//*********************************
memcached-session-manager 提供几种 session 序列化方式,一定要根据你选择的序列化方式添加依赖的 jar 包.

maven Error starting Sun’s native2ascii

Maven指向的是JRE的目录,JRE目录下没有tools.jar,故找不到native2ascii。
研究后发现Maven指定的是../相对路径按道理能通过找寻上一级文件夹从而找到jdk目录继而找到tools.jar包,因此一般情况下是用JDK里面的JRE不会出现这个问题。
通过上面分析判断项目配置的JRE不在JDK的目录,把tools.jar文件放到JRE目录lib文件夹的ext文件夹下问题解决。