forked from Viscent/javamtp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...rencyPatternInAction/src/io/github/viscent/mtpattern/ch6/promise/example/FTPUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.viscent.mtpattern.ch6.promise.example; | ||
|
||
import java.io.File; | ||
|
||
public interface FTPUploader { | ||
void init(String ftpServer, String ftpUserName, String password, | ||
String serverDir) throws Exception; | ||
|
||
void upload(File file) throws Exception; | ||
|
||
void disconnect(); | ||
} |
51 changes: 51 additions & 0 deletions
51
...ternInAction/src/io/github/viscent/mtpattern/ch6/promise/example/FTPUploaderPromisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.viscent.mtpattern.ch6.promise.example; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.FutureTask; | ||
|
||
// 模式角色:Promise.Promisor | ||
public class FTPUploaderPromisor { | ||
|
||
// 模式角色:Promise.Promisor.compute | ||
public static Future<FTPUploader> newFTPUploaderPromise(String ftpServer, | ||
String ftpUserName, String password, String serverDir) { | ||
Executor helperExecutor=new Executor() { | ||
@Override | ||
public void execute(Runnable command) { | ||
Thread t = new Thread(command); | ||
t.start(); | ||
} | ||
}; | ||
return newFTPUploaderPromise(ftpServer,ftpUserName,password,serverDir,helperExecutor); | ||
} | ||
|
||
public static Future<FTPUploader> newFTPUploaderPromise(String ftpServer, | ||
String ftpUserName, String password, String serverDir, | ||
Executor helperExecutor) { | ||
Callable<FTPUploader> callable = new Callable<FTPUploader>() { | ||
|
||
@Override | ||
public FTPUploader call() throws Exception { | ||
String implClazz = System.getProperty("ftp.client.impl"); | ||
if (null == implClazz) { | ||
implClazz = | ||
"io.github.viscent.mtpattern.ch6.promise.example.FTPClientUtil"; | ||
} | ||
FTPUploader self; | ||
self = (FTPUploader) Class.forName(implClazz).newInstance(); | ||
// self = new FTPClientUtil(); | ||
self.init(ftpServer, ftpUserName, password, serverDir); | ||
return self; | ||
} | ||
|
||
}; | ||
|
||
// task相当于模式角色:Promise.Promise | ||
final FutureTask<FTPUploader> task = new FutureTask<>(callable); | ||
helperExecutor.execute(task); | ||
return task; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...yPatternInAction/src/io/github/viscent/mtpattern/ch6/promise/example/FakeFTPUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.viscent.mtpattern.ch6.promise.example; | ||
|
||
import java.io.File; | ||
|
||
import io.github.viscent.util.Debug; | ||
import io.github.viscent.util.Tools; | ||
|
||
public class FakeFTPUploader implements FTPUploader { | ||
@Override | ||
public void upload(File file) throws Exception { | ||
Debug.info("uploading %s", file); | ||
// 模拟文件上传所需的耗时 | ||
Tools.randomPause(100, 50); | ||
} | ||
|
||
@Override | ||
public void disconnect() { | ||
// 什么也不做 | ||
} | ||
|
||
@Override | ||
public void init(String ftpServer, String ftpUserName, String password, | ||
String serverDir) throws Exception { | ||
// 什么也不做 | ||
|
||
} | ||
} |