Ejemplo n.º 1
0
	private void startHttp() throws IOException 
	{
		
		try {
			this.ioReactor = new DefaultListeningIOReactor(workerCount, serverParams);

			 EventListener serverEventListener = new EventListener() {

				// @Override
				public void connectionClosed( NHttpConnection conn) {
					// closedServerConns.decrement();
					// super.connectionClosed(conn);
//					System.out.println("connectionClosed:" + conn);
				}

				public void connectionOpen( NHttpConnection conn) {
//					System.out.println("connectionOpen:" + conn);

				}

				public void connectionTimeout( NHttpConnection conn) {
					System.out.println("connectionTimeout:" + conn);

				}

				public void fatalIOException( IOException ex,
						 NHttpConnection conn) {
//					System.out.println("fatalIOException:" + conn);
					ex.printStackTrace();
				}

				public void fatalProtocolException( HttpException ex,
						 NHttpConnection conn) {
//					System.out.println("fatalProtocolException:" + conn);
					ex.printStackTrace();

				}

			};

			final NHttpServiceHandler serviceHandler = createHttpServiceHandler(
					HttpUtil.getHttpBaseRPCIOHandler(), null,
					serverEventListener);
			this.endpoint = this.ioReactor.listen(new InetSocketAddress(
					ip, port));
			IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
					serviceHandler, serverParams);
//			this.execute(serviceHandler, ioEventDispatch);
			this.thread = new IOReactorThread(ioEventDispatch);
			this.thread.start();
			try {
				thread.join(1000);
				this.started = true;
			} catch ( InterruptedException e) {

				e.printStackTrace();
			}
			
		} catch ( IOReactorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
Ejemplo n.º 2
0
	private void startHttps() throws Exception 
	{
		
		try {
			this.ioReactor = new DefaultListeningIOReactor(workerCount, serverParams);

			EventListener serverEventListener = new EventListener() {

				// @Override
				public void connectionClosed( NHttpConnection conn) {
					// closedServerConns.decrement();
					// super.connectionClosed(conn);
//					System.out.println("connectionClosed:" + conn);
				}

				public void connectionOpen( NHttpConnection conn) {
//					System.out.println("connectionOpen:" + conn);

				}

				public void connectionTimeout( NHttpConnection conn) {
					System.out.println("connectionTimeout:" + conn);

				}

				public void fatalIOException( IOException ex,
						 NHttpConnection conn) {
//					System.out.println("fatalIOException:" + conn);
					ex.printStackTrace();
				}

				public void fatalProtocolException( HttpException ex,
						 NHttpConnection conn) {
//					System.out.println("fatalProtocolException:" + conn);
					ex.printStackTrace();

				}

			};

			final NHttpServiceHandler serviceHandler = createHttpServiceHandler(
					HttpUtil.getHttpBaseRPCIOHandler(), null,
					serverEventListener);
			this.endpoint = this.ioReactor.listen(new InetSocketAddress(
					ip, port));
			ProMap ssls =  ApplicationContext.getApplicationContext().getMapProperty("rpc.protocol.http.ssl.server");
            if(ssls == null)
            {
                throw new Exception("启用了ssl模式, 但是没有指定rpc.protocol.http.ssl.server 参数,请检查文件org/frameworkset/spi/manager-rpc-http.xml是否正确设置了该参数。");
            }
            String keyStore = ssls.getString("keyStore");
            String keyStorePassword = ssls.getString("keyStorePassword");
            String trustStore = ssls.getString("trustStore");
            String trustStorePassword = ssls.getString("trustStorePassword");
            SSLContext sslcontext = SSLHelper.createSSLContext(keyStore, keyStorePassword, trustStore, trustStorePassword);
            
			 IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
					 serviceHandler, 
		                sslcontext,
		                serverParams);
//			IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
//					serviceHandler, serverParams);
//			this.execute(serviceHandler, ioEventDispatch);
			this.thread = new IOReactorThread(ioEventDispatch);
			this.thread.start();
			try {
				thread.join(1000);
				this.started = true;
			} catch ( InterruptedException e) {

				e.printStackTrace();
			}
			
//			ClassLoader cl = this.getClass().getClassLoader();
//	        URL url = cl.getResource("test.keystore");
//	        KeyStore keystore  = KeyStore.getInstance("jks");
//	        keystore.load(url.openStream(), "nopassword".toCharArray());
//	        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
//	                KeyManagerFactory.getDefaultAlgorithm());
//	        kmfactory.init(keystore, "nopassword".toCharArray());
//	        KeyManager[] keymanagers = kmfactory.getKeyManagers(); 
//	        SSLContext sslcontext = SSLContext.getInstance("TLS");
//	        sslcontext.init(keymanagers, null, null);
//	        
	       
	        
	        // Set up request handlers
//	        HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
//	        reqistry.register("*", new HttpFileHandler(args[0]));
//	        
//	        handler.setHandlerResolver(reqistry);
//	        
//	        // Provide an event logger
//	        handler.setEventListener(new EventLogger());
//	        
//	       
//	        
//	        ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
//	        try {
//	            ioReactor.listen(new InetSocketAddress(8080));
//	            ioReactor.execute(ioEventDispatch);
//	        } catch (InterruptedIOException ex) {
//	            System.err.println("Interrupted");
//	        } catch (IOException e) {
//	            System.err.println("I/O error: " + e.getMessage());
//	        }
//	        System.out.println("Shutdown");
			
		} catch ( IOReactorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
Ejemplo n.º 3
0
  public void send(String content, HttpPost _httpPost, final Object _callPushClient) {
    sendCounter++;
    Date dateSendCounter = new Date();
    log.error(
        "PushClient: sendCounter: "
            + sendCounter
            + " "
            + "callBackCompletedCounter:"
            + callBackCompletedCounter
            + " "
            + "callBackCounter:"
            + callBackCounter
            + " "
            + dateSendCounter.getHours()
            + ":"
            + dateSendCounter.getMinutes()
            + ":"
            + dateSendCounter.getSeconds());
    try {
      StringEntity entity = null;
      try {
        entity = new StringEntity(content);
        //				entity.setContentEncoding("UTF-8");
        //				entity.setContentType("text/xml");
      } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
      }
      _httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
      _httpPost.setEntity(entity);

      int CountDownValue = PushClient.sendCounter - PushClient.callBackCompletedCounter;
      if (CountDownValue > limit) {
        creatLatch(CountDownValue);
      } else {
        flag = false;
        counterAfterBeyondLimit = 0;
      }

      //			Future<HttpResponse> future = asyClient.execute(_httpPost, new
      // FutureCallbackImpl(_callPushClient, flag, latch));
      //
      //			if(flag){
      //				System.out.println("%%%%%PushClient: latch.getCount():" + latch.getCount());
      //				Date dateBefore = new Date();
      //				try {
      //					latch.await(3, TimeUnit.SECONDS);
      //				} catch (InterruptedException e) {
      //					// TODO Auto-generated catch block
      //					e.printStackTrace();
      //				}
      //				Date dateAfter = new Date();
      //				if(((dateAfter.getTime() - dateBefore.getTime()) > 2500)){
      //					System.out.println("**********PushClient: restart asyClient!!!");
      //					log.info("**********PushClient: restart asyClient!!!");
      //					try {
      //						JmsSubscription.asyClient.shutdown();
      //						JmsSubscription.asyClient = new DefaultHttpAsyncClient();
      //						JmsSubscription.asyClient.start();
      //					} catch (InterruptedException e) {
      //						// TODO Auto-generated catch block
      //						e.printStackTrace();
      //					} catch (IOReactorException e) {
      //						// TODO Auto-generated catch block
      //						e.printStackTrace();
      //					}
      //				}
      //			}
      log.error("PushClient-flag:" + flag);
      sendThread sends = new sendThread(_httpPost, _callPushClient);
      JmsSubscription.pushpool.execute(sends);
      /*Future<HttpResponse> future = asyClient.execute(_httpPost, new FutureCallback<HttpResponse>() {

      				public void failed(Exception ex) {
      					// TODO Auto-generated method stub
      					System.out.println("Exception:" + ex.getLocalizedMessage());
      					ex.printStackTrace();

      					callBackCounter ++;
      					if(_callPushClient instanceof JmsSubscription){
      						((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      					}else if(_callPushClient instanceof SendNotification){
      						((SendNotification) _callPushClient).setSuccessfulFlag(false);
      					}
      					System.out.println("enter the failed function!!!");
      					log.error("enter the failed function!!!");
      //					log.info("enter the failed function!!!");
      				}

      				public void completed(HttpResponse result) {
      					// TODO Auto-generated method stub
      					if((flag) && (latch != null)){
      						latch.countDown();
      					}
      					callBackCounter ++;
      					callBackCompletedCounter ++;
      					int statusCode = result.getStatusLine().getStatusCode();
      					switch(statusCode){
      					case 200 :
      						if(flag){
      							counterAfterBeyondLimit ++;
      						}
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(true);
      						}
      //						System.out.println("%%%%%PushClient:status:200");
      						log.error("%%%%%PushClient:status:200 number:" + counterAfterBeyondLimit);
      						break;
      					case 201 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(true);
      						}
      						System.out.println("%%%%%PushClient:status:201");
      						log.error("%%%%%PushClient:status:201");
      						break;
      					case 202 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(true);
      						}
      						System.out.println("%%%%%PushClient:status:202");
      						log.error("%%%%%PushClient:status:202");
      						break;
      					case 204 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(true);
      						}
      						System.out.println("%%%%%PushClient:status:204");
      						log.error("%%%%%PushClient:status:204");
      						break;
      					case 400 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(false);
      						}
      						System.out.println("%%%%%PushClient:status:400");
      						log.error("%%%%%PushClient:status:400");
      						break;
      					case 401 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(false);
      						}
      						System.out.println("%%%%%PushClient:status:401");
      						log.error("%%%%%PushClient:status:401");
      						break;
      					case 403 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(false);
      						}
      						System.out.println("%%%%%PushClient:status:403");
      						log.error("%%%%%PushClient:status:403");
      						break;
      					case 404 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(false);
      						}
      						System.out.println("%%%%%PushClient:status:404");
      						log.error("%%%%%PushClient:status:404");
      						break;
      					case 500 :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(false);
      						}
      						System.out.println("%%%%%PushClient:status:500");
      						log.error("%%%%%PushClient:status:500");
      						break;
      					default :
      						if(_callPushClient instanceof JmsSubscription){
      							((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      						}else if(_callPushClient instanceof SendNotification){
      							((SendNotification) _callPushClient).setSuccessfulFlag(true);
      						}
      						System.out.println("%%%%%PushClient:status:default");
      						log.error("%%%%%PushClient:status:default");
      					}
      				}

      				public void cancelled() {
      					// TODO Auto-generated method stub
      					callBackCounter ++;
      					if(_callPushClient instanceof JmsSubscription){
      						((JmsSubscription) _callPushClient).setSuccessfulFlag(true);
      					}else if(_callPushClient instanceof SendNotification){
      						((SendNotification) _callPushClient).setSuccessfulFlag(true);
      					}
      					System.out.println("enter the cancelled function!!!");
      					log.error("enter the cancelled function!!!");
      				}
      			});
      */
      //			if(flag){
      //				Date dateBefore = new Date();
      //				log.error("dateBefore: " + dateBefore.getMinutes() + ":" + dateBefore.getSeconds());
      //				latch.await(80, TimeUnit.SECONDS);
      //				Date dateAfter = new Date();
      //				log.error("dateAfter: " + dateAfter.getMinutes() + ":" + dateAfter.getSeconds());
      //				log.error("Time-Diff:" + (dateAfter.getTime() - dateBefore.getTime()));
      //				if(((dateAfter.getTime() - dateBefore.getTime()) > 75000)){
      //					System.out.println("%%%%%PushClient: there is " + (sendCounter -
      // callBackCompletedCounter) + " callback left!!");
      //					log.error("%%%%%PushClient: there is " + (sendCounter - callBackCompletedCounter) + "
      // callback left!!");
      //					System.out.println("**********PushClient: restart asyClient!!!");
      //					log.error("**********PushClient: restart asyClient!!!");
      //					JmsSubscription.asyClient.shutdown();
      //					JmsSubscription.asyClient = null;
      //					Thread.sleep(3000);
      //					JmsSubscription.asyClient = new DefaultHttpAsyncClient();
      //					JmsSubscription.asyClient.start();
      //				}
      //				dateBefore = null;
      //				dateAfter = null;
      //			}
      //	        HttpResponse response = future.get();
      //	        System.out.println("Response: " + response.getStatusLine());
      //	        System.out.println("Shutting down")
      _httpPost.releaseConnection();
    } catch (Exception e) {
      log.error(
          "%%%%%PushClient: enter the outException in PushClient!!!" + e.getLocalizedMessage());
      e.printStackTrace();
      //	        System.out.println("Exception: " + e.getLocalizedMessage());
      //	        if(_callPushClient instanceof JmsSubscription){
      //	        	((JmsSubscription) _callPushClient).setSuccessfulFlag(false);
      //	        }else if(_callPushClient instanceof SendNotification){
      //	        	((SendNotification) _callPushClient).setSuccessfulFlag(false);
      //	        }
      try {
        JmsSubscription.asyClient.shutdown();
        PushClient.sendCounter = 0;
        PushClient.callBackCompletedCounter = 0;
        callBackCounter = 0;

        JmsSubscription.asyClient = null;
        Thread.sleep(3000);
        JmsSubscription.asyClient = new DefaultHttpAsyncClient();
        JmsSubscription.asyClient.start();
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (IOReactorException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
      }
    } finally {
      if (_httpPost != null) {
        _httpPost.releaseConnection();
      }
    }
    if (flag) {
      restart();
    }
  }