代理模式

代理模式

为另一个对象提供一个替身或占位符以访问这个对象.

代理模式中远程代理和动态代理比较常用

Elasticsearch配置ik分词器

假定你已经安装了elasticsearch2.1.0和maven, 下面的步骤针对elasticsearch2.1.0有效, 其它版本可能不使用

下载ik

因为我的elasticsearch是2.1.0,所以去这里下载https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v1.6.1

其它版本的elasticsearch对应的ik去https://github.com/medcl/elasticsearch-analysis-ik 下载

解压ik,并进入ik

执行 mvn package 编译ik

复制文件

将elasticsearch-analysis-ik-master/target/releases/elasticsearch-analysis-ik-1.6.1.zip 拷贝到es的plugin/ik目录下并解压

同时将elasticsearch-analysis-ik-master/config/ik文件夹拷贝到es的config下

然后重启es,就可以了.

Java发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.google.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
*/
public class EmailUtil {

private static Properties properties = null;

public static void init(Properties properties)
{
EmailUtil.properties = properties;
}

public static void send(String subject, String message) {
try {
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = properties.getProperty("mail.user");
String password = properties.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, authenticator);
MimeMessage mime = new MimeMessage(session);
InternetAddress form = new InternetAddress(properties.getProperty("mail.user"));
mime.setFrom(form);
InternetAddress to = new InternetAddress(properties.getProperty("main.to"));
mime.setRecipient(RecipientType.TO, to);

mime.setSubject(subject);
mime.setContent(message, "text/html;charset=UTF-8");

Transport.send(mime);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.exmail.qq.com");
properties.put("mail.user", "user1@example.com");
properties.put("mail.password", "password");
properties.put("main.to","user2@qq.com");

EmailUtil.init(properties);
EmailUtil.send("hello", "这是来自大洋彼岸的声音!");
}

}

将启动命令中包含某个字符串的进行杀死

1
2
3
4
[root@dn01 testbash]# ps -ef | grep hello
root 24794 7457 0 11:09 pts/2 00:00:00 sh hello.sh
root 24910 7457 0 11:09 pts/2 00:00:00 sh hello.sh
root 25295 7457 0 11:11 pts/2 00:00:00 grep hello

使用

1
ps aux | grep hello | grep -v grep |awk '{print $2}'| xargs kill -9

可以将上述的前两个进程杀死

Swift - 使用NSURLSession同步获取数据(通过添加信号量)

首先非常感谢代码的原作者,为了查询方便我就在这里又留了一份,请见谅.

原文http://www.hangge.com/blog/cache/detail_816.html

过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSession 代替 NSURLConnection。

如果想要 NSURLSession 也能够同步请求,即数据获取后才继续执行下面的代码,使用信号、信号量就可以实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//创建NSURL对象
let urlString:String="http://www.hangge.com"
let url:NSURL! = NSURL(string:urlString)
//创建请求对象
let request:NSURLRequest = NSURLRequest(URL: url)

let session = NSURLSession.sharedSession()

let semaphore = dispatch_semaphore_create(0)

let dataTask = session.dataTaskWithRequest(request,
completionHandler: {(data, response, error) -> Void in
if error != nil{
print(error?.code)
print(error?.description)
}else{
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(str)
}

dispatch_semaphore_signal(semaphore)
}) as NSURLSessionTask

//使用resume方法启动任务
dataTask.resume()

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
print("数据加载完毕!")
//继续执行其他代码.......