Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
be15d8ce
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
be15d8ce
编写于
5月 05, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Manually close the entity manager for async requests.
上级
f17e7ea8
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
41 addition
and
0 deletion
+41
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/utils/EntityManagerUtil.java
...in/java/com/ctrip/apollo/biz/utils/EntityManagerUtil.java
+25
-0
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/NotificationController.java
...ollo/configservice/controller/NotificationController.java
+11
-0
apollo-configservice/src/test/java/com/ctrip/apollo/configservice/controller/NotificationControllerTest.java
.../configservice/controller/NotificationControllerTest.java
+5
-0
未找到文件。
apollo-biz/src/main/java/com/ctrip/apollo/biz/utils/EntityManagerUtil.java
0 → 100644
浏览文件 @
be15d8ce
package
com.ctrip.apollo.biz.utils
;
import
org.springframework.orm.jpa.EntityManagerFactoryAccessor
;
import
org.springframework.orm.jpa.EntityManagerFactoryUtils
;
import
org.springframework.orm.jpa.EntityManagerHolder
;
import
org.springframework.stereotype.Component
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Component
public
class
EntityManagerUtil
extends
EntityManagerFactoryAccessor
{
/**
* close the entity manager.
* Use it with caution! This is only intended for use with async request, which Spring won't
* close the entity manager until the async request is finished.
*/
public
void
closeEntityManager
()
{
EntityManagerHolder
emHolder
=
(
EntityManagerHolder
)
TransactionSynchronizationManager
.
getResource
(
getEntityManagerFactory
());
logger
.
debug
(
"Closing JPA EntityManager in EntityManagerUtil"
);
EntityManagerFactoryUtils
.
closeEntityManager
(
emHolder
.
getEntityManager
());
}
}
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/NotificationController.java
浏览文件 @
be15d8ce
...
...
@@ -12,6 +12,7 @@ import com.ctrip.apollo.biz.entity.AppNamespace;
import
com.ctrip.apollo.biz.message.MessageListener
;
import
com.ctrip.apollo.biz.message.Topics
;
import
com.ctrip.apollo.biz.service.AppNamespaceService
;
import
com.ctrip.apollo.biz.utils.EntityManagerUtil
;
import
com.ctrip.apollo.core.ConfigConsts
;
import
com.ctrip.apollo.core.dto.ApolloConfigNotification
;
import
com.dianping.cat.Cat
;
...
...
@@ -49,6 +50,9 @@ public class NotificationController implements MessageListener {
@Autowired
private
AppNamespaceService
appNamespaceService
;
@Autowired
private
EntityManagerUtil
entityManagerUtil
;
@RequestMapping
(
method
=
RequestMethod
.
GET
)
public
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
pollNotification
(
@RequestParam
(
value
=
"appId"
)
String
appId
,
...
...
@@ -94,6 +98,13 @@ public class NotificationController implements MessageListener {
String
dataCenter
)
{
List
<
String
>
publicWatchedKeys
=
Lists
.
newArrayList
();
AppNamespace
appNamespace
=
appNamespaceService
.
findByNamespaceName
(
namespace
);
/**
* Manually close the entity manager.
* Since for async request, Spring won't do so until the request is finished,
* which is unacceptable since we are doing long polling - means the db connection would be hold
* for a very long time
*/
entityManagerUtil
.
closeEntityManager
();
//check whether the namespace's appId equals to current one
if
(
Objects
.
isNull
(
appNamespace
)
||
Objects
.
equals
(
applicationId
,
appNamespace
.
getAppId
()))
{
...
...
apollo-configservice/src/test/java/com/ctrip/apollo/configservice/controller/NotificationControllerTest.java
浏览文件 @
be15d8ce
...
...
@@ -7,6 +7,7 @@ import com.google.common.collect.Multimap;
import
com.ctrip.apollo.biz.entity.AppNamespace
;
import
com.ctrip.apollo.biz.message.Topics
;
import
com.ctrip.apollo.biz.service.AppNamespaceService
;
import
com.ctrip.apollo.biz.utils.EntityManagerUtil
;
import
com.ctrip.apollo.core.ConfigConsts
;
import
com.ctrip.apollo.core.dto.ApolloConfigNotification
;
...
...
@@ -15,6 +16,7 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.util.ReflectionTestUtils
;
...
...
@@ -39,6 +41,8 @@ public class NotificationControllerTest {
private
String
someDataCenter
;
@Mock
private
AppNamespaceService
appNamespaceService
;
@Mock
private
EntityManagerUtil
entityManagerUtil
;
private
Multimap
<
String
,
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>>
deferredResults
;
...
...
@@ -46,6 +50,7 @@ public class NotificationControllerTest {
public
void
setUp
()
throws
Exception
{
controller
=
new
NotificationController
();
ReflectionTestUtils
.
setField
(
controller
,
"appNamespaceService"
,
appNamespaceService
);
ReflectionTestUtils
.
setField
(
controller
,
"entityManagerUtil"
,
entityManagerUtil
);
someAppId
=
"someAppId"
;
someCluster
=
"someCluster"
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录