org.springframework.beans.factory.BeanClassLoaderAware#org.springframework.remoting.support.RemoteInvocationResult源码实例Demo

下面列出了org.springframework.beans.factory.BeanClassLoaderAware#org.springframework.remoting.support.RemoteInvocationResult 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Execute the given remote invocation, sending an invoker request message
 * to this accessor's target queue and waiting for a corresponding response.
 * @param invocation the RemoteInvocation to execute
 * @return the RemoteInvocationResult object
 * @throws JMSException in case of JMS failure
 * @see #doExecuteRequest
 */
protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws JMSException {
	Connection con = createConnection();
	Session session = null;
	try {
		session = createSession(con);
		Queue queueToUse = resolveQueue(session);
		Message requestMessage = createRequestMessage(session, invocation);
		con.start();
		Message responseMessage = doExecuteRequest(session, queueToUse, requestMessage);
		if (responseMessage != null) {
			return extractInvocationResult(responseMessage);
		}
		else {
			return onReceiveTimeout(invocation);
		}
	}
	finally {
		JmsUtils.closeSession(session);
		ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), true);
	}
}
 
/**
 * Execute the given remote invocation, sending an invoker request message
 * to this accessor's target queue and waiting for a corresponding response.
 * @param invocation the RemoteInvocation to execute
 * @return the RemoteInvocationResult object
 * @throws JMSException in case of JMS failure
 * @see #doExecuteRequest
 */
protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws JMSException {
	Connection con = createConnection();
	Session session = null;
	try {
		session = createSession(con);
		Queue queueToUse = resolveQueue(session);
		Message requestMessage = createRequestMessage(session, invocation);
		con.start();
		Message responseMessage = doExecuteRequest(session, queueToUse, requestMessage);
		if (responseMessage != null) {
			return extractInvocationResult(responseMessage);
		}
		else {
			return onReceiveTimeout(invocation);
		}
	}
	finally {
		JmsUtils.closeSession(session);
		ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), true);
	}
}
 
/**
 * Execute the given remote invocation, sending an invoker request message
 * to this accessor's target queue and waiting for a corresponding response.
 * @param invocation the RemoteInvocation to execute
 * @return the RemoteInvocationResult object
 * @throws JMSException in case of JMS failure
 * @see #doExecuteRequest
 */
protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws JMSException {
	Connection con = createConnection();
	Session session = null;
	try {
		session = createSession(con);
		Queue queueToUse = resolveQueue(session);
		Message requestMessage = createRequestMessage(session, invocation);
		con.start();
		Message responseMessage = doExecuteRequest(session, queueToUse, requestMessage);
		if (responseMessage != null) {
			return extractInvocationResult(responseMessage);
		}
		else {
			return onReceiveTimeout(invocation);
		}
	}
	finally {
		JmsUtils.closeSession(session);
		ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), true);
	}
}
 
/**
 * Execute the given request through the HttpClient.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #createHttpPost
 * @see #setRequestBody
 * @see #executeHttpPost
 * @see #validateResponse
 * @see #getResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpPost postMethod = createHttpPost(config);
	setRequestBody(config, postMethod, baos);
	try {
		HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
		validateResponse(config, response);
		InputStream responseBody = getResponseBody(config, response);
		return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
	}
	finally {
		postMethod.releaseConnection();
	}
}
 
源代码5 项目: cuba   文件: HttpServiceProxy.java
@Override
protected Object recreateRemoteInvocationResult(RemoteInvocationResult result) throws Throwable {
    Throwable throwable = result.getException();
    if (throwable != null) {
        if (throwable instanceof InvocationTargetException)
            throwable = ((InvocationTargetException) throwable).getTargetException();
        if (throwable instanceof RemoteException) {
            Exception exception = ((RemoteException) throwable).getFirstCauseException();
            // This is a checked exception declared in a service method
            // or runtime exception supported by client
            if (exception != null) {
                RemoteInvocationUtils.fillInClientStackTraceIfPossible(exception);
                throw exception;
            }
        }
    }
    return super.recreateRemoteInvocationResult(result);
}
 
/**
 * Execute the given request through the HttpClient.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #createHttpPost
 * @see #setRequestBody
 * @see #executeHttpPost
 * @see #validateResponse
 * @see #getResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpPost postMethod = createHttpPost(config);
	setRequestBody(config, postMethod, baos);
	try {
		HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
		validateResponse(config, response);
		InputStream responseBody = getResponseBody(config, response);
		return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
	}
	finally {
		postMethod.releaseConnection();
	}
}
 
/**
 * Execute the given request through the HttpClient.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #createHttpPost
 * @see #setRequestBody
 * @see #executeHttpPost
 * @see #validateResponse
 * @see #getResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpPost postMethod = createHttpPost(config);
	setRequestBody(config, postMethod, baos);
	try {
		HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
		validateResponse(config, response);
		InputStream responseBody = getResponseBody(config, response);
		return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
	}
	finally {
		postMethod.releaseConnection();
	}
}
 
@Override
public final RemoteInvocationResult executeRequest(
		HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws Exception {

	ByteArrayOutputStream baos = getByteArrayOutputStream(invocation);
	if (logger.isDebugEnabled()) {
		logger.debug("Sending HTTP invoker request for service at [" + config.getServiceUrl() +
				"], with size " + baos.size());
	}
	return doExecuteRequest(config, baos);
}
 
/**
 * Execute the given request through a standard J2SE HttpURLConnection.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #openConnection
 * @see #prepareConnection
 * @see #writeRequestBody
 * @see #validateResponse
 * @see #readResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpURLConnection con = openConnection(config);
	prepareConnection(con, baos.size());
	writeRequestBody(config, con, baos);
	validateResponse(config, con);
	InputStream responseBody = readResponseBody(config, con);

	return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
}
 
/**
 * Write the given RemoteInvocationResult to the given HTTP response.
 * @param request current HTTP request
 * @param response current HTTP response
 * @param result the RemoteInvocationResult object
 * @throws IOException in case of I/O failure
 */
protected void writeRemoteInvocationResult(
		HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result)
		throws IOException {

	response.setContentType(getContentType());
	writeRemoteInvocationResult(request, response, result, response.getOutputStream());
}
 
@Bean
public HttpInvokerProxyFactoryBean myService() {
	String name = env.getProperty("testbean.name");
	HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean();
	factory.setServiceUrl("/svc/" + name);
	factory.setServiceInterface(MyService.class);
	factory.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
		@Override
		public RemoteInvocationResult executeRequest(HttpInvokerClientConfiguration config, RemoteInvocation invocation) {
			return new RemoteInvocationResult(null);
		}
	});
	return factory;
}
 
@Bean
public HttpInvokerProxyFactoryBean myService() {
	HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean();
	factory.setServiceUrl("/svc/dummy");
	factory.setServiceInterface(MyService.class);
	factory.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
		@Override
		public RemoteInvocationResult executeRequest(HttpInvokerClientConfiguration config, RemoteInvocation invocation) {
			return new RemoteInvocationResult(null);
		}
	});
	return factory;
}
 
/**
 * Write the given RemoteInvocationResult to the given HTTP response.
 * @param exchange current HTTP request/response
 * @param result the RemoteInvocationResult object
 * @throws java.io.IOException in case of I/O failure
 */
protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result)
		throws IOException {

	exchange.getResponseHeaders().set("Content-Type", getContentType());
	exchange.sendResponseHeaders(200, 0);
	writeRemoteInvocationResult(exchange, result, exchange.getResponseBody());
}
 
/**
 * Execute the given request through a standard {@link HttpURLConnection}.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #openConnection
 * @see #prepareConnection
 * @see #writeRequestBody
 * @see #validateResponse
 * @see #readResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpURLConnection con = openConnection(config);
	prepareConnection(con, baos.size());
	writeRequestBody(config, con, baos);
	validateResponse(config, con);
	InputStream responseBody = readResponseBody(config, con);

	return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
}
 
@Bean
public HttpInvokerProxyFactoryBean myService() {
	HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean();
	factory.setServiceUrl("/svc/dummy");
	factory.setServiceInterface(MyService.class);
	factory.setHttpInvokerRequestExecutor((config, invocation) -> new RemoteInvocationResult());
	return factory;
}
 
源代码16 项目: spring-analysis-note   文件: HttpInvokerTests.java
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
	TestBean target = new TestBean("myname", 99);

	final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
	exporter.setServiceInterface(ITestBean.class);
	exporter.setService(target);
	exporter.afterPropertiesSet();

	HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
	pfb.setServiceInterface(ITestBean.class);
	pfb.setServiceUrl("https://myurl");

	pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
		@Override
		public RemoteInvocationResult executeRequest(
				HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
			throw new IOException("argh");
		}
	});

	pfb.afterPropertiesSet();
	ITestBean proxy = (ITestBean) pfb.getObject();
	try {
		proxy.setAge(50);
		fail("Should have thrown RemoteAccessException");
	}
	catch (RemoteAccessException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
	}
}
 
@Override
public void onMessage(Message requestMessage, Session session) throws JMSException {
	RemoteInvocation invocation = readRemoteInvocation(requestMessage);
	if (invocation != null) {
		RemoteInvocationResult result = invokeAndCreateResult(invocation, this.proxy);
		writeRemoteInvocationResult(requestMessage, session, result);
	}
}
 
/**
 * Send the given RemoteInvocationResult as a JMS message to the originator.
 * @param requestMessage current request message
 * @param session the JMS Session to use
 * @param result the RemoteInvocationResult object
 * @throws javax.jms.JMSException if thrown by trying to send the message
 */
protected void writeRemoteInvocationResult(
		Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {

	Message response = createResponseMessage(requestMessage, session, result);
	MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
	try {
		producer.send(response);
	}
	finally {
		JmsUtils.closeMessageProducer(producer);
	}
}
 
@Override
public final RemoteInvocationResult executeRequest(
		HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws Exception {

	ByteArrayOutputStream baos = getByteArrayOutputStream(invocation);
	if (logger.isDebugEnabled()) {
		logger.debug("Sending HTTP invoker request for service at [" + config.getServiceUrl() +
				"], with size " + baos.size());
	}
	return doExecuteRequest(config, baos);
}
 
/**
 * Reads a remote invocation from the request, executes it,
 * and writes the remote invocation result to the response.
 * @see #readRemoteInvocation(com.sun.net.httpserver.HttpExchange)
 * @see #invokeAndCreateResult(org.springframework.remoting.support.RemoteInvocation, Object)
 * @see #writeRemoteInvocationResult(com.sun.net.httpserver.HttpExchange, org.springframework.remoting.support.RemoteInvocationResult)
 */
@Override
public void handle(HttpExchange exchange) throws IOException {
	try {
		RemoteInvocation invocation = readRemoteInvocation(exchange);
		RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
		writeRemoteInvocationResult(exchange, result);
		exchange.close();
	}
	catch (ClassNotFoundException ex) {
		exchange.sendResponseHeaders(500, -1);
		logger.error("Class not found during deserialization", ex);
	}
}
 
源代码21 项目: spring4-understanding   文件: HttpInvokerTests.java
@Test
public void httpInvokerWithSpecialLocalMethods() throws Exception {
	String serviceUrl = "http://myurl";
	HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
	pfb.setServiceInterface(ITestBean.class);
	pfb.setServiceUrl(serviceUrl);

	pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
		@Override
		public RemoteInvocationResult executeRequest(
				HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
			throw new IOException("argh");
		}
	});

	pfb.afterPropertiesSet();
	ITestBean proxy = (ITestBean) pfb.getObject();

	// shouldn't go through to remote service
	assertTrue(proxy.toString().indexOf("HTTP invoker") != -1);
	assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
	assertEquals(proxy.hashCode(), proxy.hashCode());
	assertTrue(proxy.equals(proxy));

	// should go through
	try {
		proxy.setAge(50);
		fail("Should have thrown RemoteAccessException");
	}
	catch (RemoteAccessException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
	}
}
 
/**
 * Write the given RemoteInvocationResult to the given HTTP response.
 * @param exchange current HTTP request/response
 * @param result the RemoteInvocationResult object
 * @throws java.io.IOException in case of I/O failure
 */
protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result)
		throws IOException {

	exchange.getResponseHeaders().set("Content-Type", getContentType());
	exchange.sendResponseHeaders(200, 0);
	writeRemoteInvocationResult(exchange, result, exchange.getResponseBody());
}
 
/**
 * Reads a remote invocation from the request, executes it,
 * and writes the remote invocation result to the response.
 * @see #readRemoteInvocation(HttpServletRequest)
 * @see #invokeAndCreateResult(org.springframework.remoting.support.RemoteInvocation, Object)
 * @see #writeRemoteInvocationResult(HttpServletRequest, HttpServletResponse, RemoteInvocationResult)
 */
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	try {
		RemoteInvocation invocation = readRemoteInvocation(request);
		RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
		writeRemoteInvocationResult(request, response, result);
	}
	catch (ClassNotFoundException ex) {
		throw new NestedServletException("Class not found during deserialization", ex);
	}
}
 
/**
 * Write the given RemoteInvocationResult to the given HTTP response.
 * @param request current HTTP request
 * @param response current HTTP response
 * @param result the RemoteInvocationResult object
 * @throws IOException in case of I/O failure
 */
protected void writeRemoteInvocationResult(
		HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result)
		throws IOException {

	response.setContentType(getContentType());
	writeRemoteInvocationResult(request, response, result, response.getOutputStream());
}
 
/**
 * Reads a remote invocation from the request, executes it,
 * and writes the remote invocation result to the response.
 * @see #readRemoteInvocation(HttpExchange)
 * @see #invokeAndCreateResult(RemoteInvocation, Object)
 * @see #writeRemoteInvocationResult(HttpExchange, RemoteInvocationResult)
 */
@Override
public void handle(HttpExchange exchange) throws IOException {
	try {
		RemoteInvocation invocation = readRemoteInvocation(exchange);
		RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
		writeRemoteInvocationResult(exchange, result);
		exchange.close();
	}
	catch (ClassNotFoundException ex) {
		exchange.sendResponseHeaders(500, -1);
		logger.error("Class not found during deserialization", ex);
	}
}
 
/**
 * Write the given RemoteInvocationResult to the given HTTP response.
 * @param exchange current HTTP request/response
 * @param result the RemoteInvocationResult object
 * @throws java.io.IOException in case of I/O failure
 */
protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result)
		throws IOException {

	exchange.getResponseHeaders().set("Content-Type", getContentType());
	exchange.sendResponseHeaders(200, 0);
	writeRemoteInvocationResult(exchange, result, exchange.getResponseBody());
}
 
/**
 * Execute the given request through a standard {@link HttpURLConnection}.
 * <p>This method implements the basic processing workflow:
 * The actual work happens in this class's template methods.
 * @see #openConnection
 * @see #prepareConnection
 * @see #writeRequestBody
 * @see #validateResponse
 * @see #readResponseBody
 */
@Override
protected RemoteInvocationResult doExecuteRequest(
		HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
		throws IOException, ClassNotFoundException {

	HttpURLConnection con = openConnection(config);
	prepareConnection(con, baos.size());
	writeRequestBody(config, con, baos);
	validateResponse(config, con);
	InputStream responseBody = readResponseBody(config, con);

	return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
}
 
@Bean
public HttpInvokerProxyFactoryBean myService() {
	HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean();
	factory.setServiceUrl("/svc/dummy");
	factory.setServiceInterface(MyService.class);
	factory.setHttpInvokerRequestExecutor((config, invocation) -> new RemoteInvocationResult());
	return factory;
}
 
源代码29 项目: spring4-understanding   文件: HttpInvokerTests.java
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
	TestBean target = new TestBean("myname", 99);

	final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
	exporter.setServiceInterface(ITestBean.class);
	exporter.setService(target);
	exporter.afterPropertiesSet();

	HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
	pfb.setServiceInterface(ITestBean.class);
	pfb.setServiceUrl("http://myurl");

	pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
		@Override
		public RemoteInvocationResult executeRequest(
				HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
			throw new IOException("argh");
		}
	});

	pfb.afterPropertiesSet();
	ITestBean proxy = (ITestBean) pfb.getObject();
	try {
		proxy.setAge(50);
		fail("Should have thrown RemoteAccessException");
	}
	catch (RemoteAccessException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
	}
}
 
@Override
public void onMessage(Message requestMessage, Session session) throws JMSException {
	RemoteInvocation invocation = readRemoteInvocation(requestMessage);
	if (invocation != null) {
		RemoteInvocationResult result = invokeAndCreateResult(invocation, this.proxy);
		writeRemoteInvocationResult(requestMessage, session, result);
	}
}