我正在尝试实现一个侦听Socket连接的简单资源适配器。我已经设法使其与普通套接字一起使用,但是在尝试使其与SSL套接字一起使用时我被卡住了。套接字实际上正在运行并且似乎已建立连接,我可以在客户端上获得输入流和输出流,但是无论如何,每当我在客户端上写入outputstream时,服务器就永远不会收到它。
这是相关的代码(是的,我知道这不是最好的实现,但是我只是想在尝试更复杂的解决方案之前完成一般的工作):
R
这一类是侦听套接字连接请求并接受它的类。
public class SocketResAdapter implements ResourceAdapter {
private SocketListener listener = null;
private final Logger logger = Logger.getLogger(SocketResAdapter.class.getName());
@Override
public void start(BootstrapContext context) throws ResourceAdapterInternalException {
logger.info("Starting resource adapter for socket");
listener = new SocketListener(context.getWorkManager());
}
@Override
public void stop() {
listener.release();
}
}
然后由其他线程为客户端提供服务:
public class SocketListener implements Work {
private final WorkManager workManager;
private SSLServerSocket serverSocket;
private int port = 9999;
private String IP = "localhost";
private final Logger logger = Logger.getLogger(SocketListener.class.getName());
private AtomicBoolean isRunning = new AtomicBoolean(false);
final boolean isRunning() {
return isRunning.get();
}
final void setRunning(Boolean running) {
isRunning.set(running);
}
public SocketListener(WorkManager manager){
this.workManager = manager;
try {
manager.startWork(this);
} catch (WorkException e) {
e.printStackTrace();
}
}
@Override
public void run() {
setRunning(true);
try {
// for ssl
System.setProperty("javax.net.ssl.keyStore", "path/to/my/keystore/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "someweirdpassword");
InetAddress inetAddress = InetAddress.getByName(IP);
SSLServerSocketFactory l_serverSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
serverSocket = (SSLServerSocket) l_serverSocketFactory.createServerSocket(port, 50, inetAddress);
while (isRunning())
{
logger.info("Accepting request from client");
SSLSocket socket = (SSLSocket) serverSocket.accept();
socket.setEnabledProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2" });
workManager.scheduleWork(new SocketProcessor(socket));
}
}catch (Exception e){
setRunning(false);
}
}
@Override
public void release() {
setRunning(false);
try {
if (serverSocket!=null){
serverSocket.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
这是相关的客户代码
public class SocketProcessor implements Work {
private Socket socket;
private AtomicBoolean isRunning = new AtomicBoolean(false);
private final Logger logger = Logger.getLogger(SocketProcessor.class.getName());
final boolean isRunning() {
return isRunning.get();
}
final void setRunning(Boolean running) {
isRunning.set(running);
}
public SocketProcessor(Socket socket) throws IOException {
this.socket = socket;
}
@Override
public void run() {
setRunning(true);
logger.info("Running socket processor");
try{
LineNumberReader in = new LineNumberReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
final PrintStream socketOutput = new PrintStream(socket.getOutputStream());
String line;
int size = 0;
while ((line = in.readLine()) != null) {
logger.info("Reading line");
if ( "EXIT".equals( line )){
socket.close();
break;
}else{
socketOutput.println(line);
size += line.length();
socketOutput.flush();
}
}
}catch (Exception e){
e.printStackTrace();
setRunning(false);
}
finally {
closeSocket();
}
}
@Override
public void release() {
setRunning(false);
closeSocket();
}
private void closeSocket(){
try {
if(!socket.isClosed()) {
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
我成功获取了会话,并且打印套接字信息已经显示了接受的套件和协议。客户端站点和服务器站点都没有错误,服务器根本没有从客户端发送数据。我想知道我做错了什么。顺便说一下,我正在Weblogic上运行适配器。