ext direct spring Configuration

Configuration

通过类ch.ralscha.extdirectspring.controller.Configuration可以方便的微调ExtDirectSpring。

RemotingProvider Configuration

可以通过三个属性timeout, maxRetries and enableBuffer配置RemotingProvider。

<bean id="extDirectSpringConfiguration" 
      class="ch.ralscha.extdirectspring.controller.Configuration" 
      p:timeout="12000" p:maxRetries="10" p:enableBuffer="false"/>

如果指定了这些参数,这些参数将会成为api.js的一部分。
上面配置调用api-debug.js的输出例子如下。

Ext.app.REMOTING_API = {
  "url" : "/controller/router",
  "type" : "remoting",
  "actions" : {
    "treeProvider" : [ {
      "name" : "getTree",
      "len" : 1
    } ]
  },
  "timeout" : 12000,
  "maxRetries" : 10,
  "enableBuffer" : false
};

如果没有特殊配置api.js将不会包含这些属性。RemotingProvider将会用默认的配置:timeout=30000, maxRetries=1和enableBuffer=10


providerType (since 1.2.2)

可以全局设置Configuration.providerType属性。Extjs使用这个值找到对应的Provider"类"(第一个字母大写,加“Provider”)默认值是'remoting',并在Ext JS中创建一个Ext.direct.RemotingProvider的。

如下创建了Ext.direct.SampleProvider,并在Ext Direct调用。

<bean id="extDirectSpringConfiguration" 
      class="ch.ralscha.extdirectspring.controller.Configuration" 
      p:providerType="sample"/>

输出api-debug.js如下:

Ext.app.REMOTING_API = {
  "url" : "/demo/router",
  "type" : "sample",
  "actions" : {
    ...
    } ]
  }
};

synchronizeOnSession会话同步
默认为false。将此属性设置为true,将会一个同步块中调用一个@ ExtDirectMethod。这是非常有用的,当服务器的方法需要操纵会话对象。

<bean id="extDirectSpringConfiguration" 
      class="ch.ralscha.extdirectspring.controller.Configuration" 
      p:synchronizeOnSession="true"

除了全局开启此功能,每一个方法都可以单独指定synchronizeOnSession在@ExtDirectMethod注解。

@ExtDirectMethod(value = ExtDirectMethodType.STORE_MODIFY, synchronizeOnSession=true)
public List<User> create(List<User> newUsers) {
  //...
}

FORM_POST方法不支持这个功能,如果你按老的格式写ExtDirect方法,如果你按新的1.2.1的格式支持。

streamResponse (since 1.1.0)

 这个特性需要特殊指出的是 Jackson ObjectMapper应该像1.0.x的版本那样直接将返回写到http servlet response并且不设置http header Content-Length (**true**)。如果设置为false(默认值)ObjectMapper 先写返回到内部buffer,设置Content-Length header ,然后将要返回响应写道http servlet response。

除了可以全局开启这个功能设置,每个方法可以独立设置streamResponse属性在注释@ExtDirectMethod

Content-Type for api.js (since 1.2.1)

默认请求 api.js和api-debug.js的返回的Content-Type是"application/javascript"。这个值可以改变通过设置 jsContentType。

<bean id="extDirectSpringConfiguration" 
      class="ch.ralscha.extdirectspring.controller.Configuration" 
      p:jsContentType="application/x-javascript"/>

或用JavaConfig

@Bean
public ch.ralscha.extdirectspring.controller.Configuration configuration() {
    ch.ralscha.extdirectspring.controller.Configuration config = new ch.ralscha.extdirectspring.controller.Configuration();
    config.setJsContentType("application/x-javascript")
    return config;
}

Error Messages

没有特殊配置,服务器发生异常,返回到客户端消息包含message“Server Error”和type“exception”。

[
  {
    "method": "method4",
    "action": "remoteProviderSimple",
    "tid": 2,
    "message": "Server Error",
    "type": "exception"
  }
]

调整返回的消息需要增加一个bean id为“extDirectSpringConfiguration”和class为“ch.ralscha.extdirectspring.controller.Configuration”到spring上下文。

<context:component-scan base-package="ch.ralscha.extdirectspring" />
<mvc:annotation-driven />

<bean id="extDirectSpringConfiguration" 
      class="ch.ralscha.extdirectspring.controller.Configuration" 
      p:defaultExceptionMessage="Panic!!!"
      p:sendExceptionMessage="false"
      p:sendStacktrace="true">
  <property name="exceptionToMessage">
    <map>
      <entry key="java.lang.IllegalArgumentException" 
             value="illegal argument"/>
      <entry key="org.springframework.beans.factory.NoSuchBeanDefinitionException">
        <null/>
      </entry>
    </map>
  </property>

</bean>

 

defaultExceptionMessageDefines the default exception message. Field *message* in the responseDefault: 'Server Error'
sendExceptionMessageIf true sends exception.getMessage() back. Field *message* in the responseDefault: false
sendStacktraceIf true sends the whole stacktrace in the field *where* backDefault: false
exceptionToMessageMapping from exception class (key) to message (value)Default: empty

消息返回规则:
1.如果有对应的exception在exceptionToMessage并且值不是null返回此值。
2.如果有对应的exception在exceptionToMessage并且值是null返回exception.getMessage()。
3.如果没有对应的exception在exceptionToMessage,并且sendExceptionMessage是true,返回exception.getMessage()。
4.如果没有对应的exception在exceptionToMessage,并且sendExceptionMessage是false,返回defaultExceptionMessage。

Concurrent execution of batched method calls 并发执行批处理方法调用

如果该功能被禁用(默认),在请求处理线程上一个一个执行批处理方法。这可能是一个问题,如果一个方法执行很长时间,后面的方法将会一直处于等待状态。

通过启用此功能可以解决这个问题,它可以并发执行方法。要启用它,设置并发batchedMethodsExecutionPolicy,与batchedMethodsExecutorService指定一个线程池。如果没有指定线程池,默认创建有5个线程(Executors.newFixedThreadPool(5))的线程池。

<bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
  <property name="corePoolSize" value="50" />
  <property name="maxPoolSize" value="200" />
  <property name="queueCapacity" value="5000" />
</bean>

<bean id="extDirectSpringConfiguration" class="ch.ralscha.extdirectspring.controller.Configuration">
    <property name="batchedMethodsExecutionPolicy" value="CONCURRENT" />
    <property name="batchedMethodsExecutorService" ref="threadPool"/>
</bean>

JavaConfig

@Bean
public ThreadPoolExecutorFactoryBean threadPoolExecutorFactoryBean() {
    ThreadPoolExecutorFactoryBean factory = new ThreadPoolExecutorFactoryBean();
    factory.setCorePoolSize(50);
    factory.setMaxPoolSize(200);
    factory.setQueueCapacity(5000);
    return factory;
}
@Bean
public ch.ralscha.extdirectspring.controller.Configuration configuration() throws Exception {
    ch.ralscha.extdirectspring.controller.Configuration config = new ch.ralscha.extdirectspring.controller.Configuration();
    config.setBatchedMethodsExecutionPolicy(BatchedMethodsExecutionPolicy.CONCURRENT);
    config.setBatchedMethodsExecutorService(threadPoolExecutorFactoryBean().getObject());
    return config;
}


请注意,启用此功能会降低性能,在某些情况下,由于线程处理开销。没有做任何性能测试情况下不要启用此功能。如果启用此功能,客户端只有一个方法调用类库执行方法在请求处理线程,并且没有线程池。在那种情况下没有线程处理性能损失。

备注:Extjs不能批量调用表单post方法,因此对哪类方法没有批量调用支持。

Spring Security
如果你用Spring Security和并发方法执行,你必须添加下面的代码在某些地方,保证在程序启动时执行。

SecurityContextHolder.setStrategyName("MODE_INHERITABLETHREADLOCAL");




 

 

 

 

 



 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值