Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
apollo
Commits
790ee89d
Commit
790ee89d
authored
Nov 17, 2018
by
nobodyiam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use weak reference to hold bean objects so that they could be garbage collected
parent
d93a6ba4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
4 deletions
+62
-4
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValue.java
...m/ctrip/framework/apollo/spring/property/SpringValue.java
+20
-3
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java
...framework/apollo/spring/property/SpringValueRegistry.java
+42
-1
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValue.java
View file @
790ee89d
package
com
.
ctrip
.
framework
.
apollo
.
spring
.
property
;
import
java.lang.ref.WeakReference
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
...
...
@@ -16,7 +17,7 @@ public class SpringValue {
private
MethodParameter
methodParameter
;
private
Field
field
;
private
Object
bean
;
private
WeakReference
<
Object
>
beanRef
;
private
String
beanName
;
private
String
key
;
private
String
placeholder
;
...
...
@@ -25,7 +26,7 @@ public class SpringValue {
private
boolean
isJson
;
public
SpringValue
(
String
key
,
String
placeholder
,
Object
bean
,
String
beanName
,
Field
field
,
boolean
isJson
)
{
this
.
bean
=
bean
;
this
.
bean
Ref
=
new
WeakReference
<>(
bean
)
;
this
.
beanName
=
beanName
;
this
.
field
=
field
;
this
.
key
=
key
;
...
...
@@ -38,7 +39,7 @@ public class SpringValue {
}
public
SpringValue
(
String
key
,
String
placeholder
,
Object
bean
,
String
beanName
,
Method
method
,
boolean
isJson
)
{
this
.
bean
=
bean
;
this
.
bean
Ref
=
new
WeakReference
<>(
bean
)
;
this
.
beanName
=
beanName
;
this
.
methodParameter
=
new
MethodParameter
(
method
,
0
);
this
.
key
=
key
;
...
...
@@ -60,6 +61,10 @@ public class SpringValue {
}
private
void
injectField
(
Object
newVal
)
throws
IllegalAccessException
{
Object
bean
=
beanRef
.
get
();
if
(
bean
==
null
)
{
return
;
}
boolean
accessible
=
field
.
isAccessible
();
field
.
setAccessible
(
true
);
field
.
set
(
bean
,
newVal
);
...
...
@@ -68,6 +73,10 @@ public class SpringValue {
private
void
injectMethod
(
Object
newVal
)
throws
InvocationTargetException
,
IllegalAccessException
{
Object
bean
=
beanRef
.
get
();
if
(
bean
==
null
)
{
return
;
}
methodParameter
.
getMethod
().
invoke
(
bean
,
newVal
);
}
...
...
@@ -103,8 +112,16 @@ public class SpringValue {
return
isJson
;
}
boolean
isTargetBeanValid
()
{
return
beanRef
.
get
()
!=
null
;
}
@Override
public
String
toString
()
{
Object
bean
=
beanRef
.
get
();
if
(
bean
==
null
)
{
return
""
;
}
if
(
isField
())
{
return
String
.
format
(
"key: %s, beanName: %s, field: %s.%s"
,
key
,
beanName
,
bean
.
getClass
().
getName
(),
field
.
getName
());
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java
View file @
790ee89d
package
com
.
ctrip
.
framework
.
apollo
.
spring
.
property
;
import
com.ctrip.framework.apollo.core.utils.ApolloThreadFactory
;
import
com.google.common.collect.LinkedListMultimap
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Multimap
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
org.springframework.beans.factory.BeanFactory
;
public
class
SpringValueRegistry
{
private
static
final
long
CLEAN_INTERVAL_IN_SECONDS
=
5
;
private
final
Map
<
BeanFactory
,
Multimap
<
String
,
SpringValue
>>
registry
=
Maps
.
newConcurrentMap
();
private
final
AtomicBoolean
initialized
=
new
AtomicBoolean
(
false
);
private
final
Object
LOCK
=
new
Object
();
public
void
register
(
BeanFactory
beanFactory
,
String
key
,
SpringValue
springValue
)
{
...
...
@@ -22,6 +29,11 @@ public class SpringValueRegistry {
}
registry
.
get
(
beanFactory
).
put
(
key
,
springValue
);
// lazy initialize
if
(
initialized
.
compareAndSet
(
false
,
true
))
{
initialize
();
}
}
public
Collection
<
SpringValue
>
get
(
BeanFactory
beanFactory
,
String
key
)
{
...
...
@@ -31,4 +43,33 @@ public class SpringValueRegistry {
}
return
beanFactorySpringValues
.
get
(
key
);
}
private
void
initialize
()
{
Executors
.
newSingleThreadScheduledExecutor
(
ApolloThreadFactory
.
create
(
"SpringValueRegistry"
,
true
)).
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
scanAndClean
();
}
catch
(
Throwable
ex
)
{
ex
.
printStackTrace
();
}
}
},
CLEAN_INTERVAL_IN_SECONDS
,
CLEAN_INTERVAL_IN_SECONDS
,
TimeUnit
.
SECONDS
);
}
private
void
scanAndClean
()
{
Iterator
<
Multimap
<
String
,
SpringValue
>>
iterator
=
registry
.
values
().
iterator
();
while
(!
Thread
.
currentThread
().
isInterrupted
()
&&
iterator
.
hasNext
())
{
Multimap
<
String
,
SpringValue
>
springValues
=
iterator
.
next
();
Iterator
<
Entry
<
String
,
SpringValue
>>
springValueIterator
=
springValues
.
entries
().
iterator
();
while
(
springValueIterator
.
hasNext
())
{
Entry
<
String
,
SpringValue
>
springValue
=
springValueIterator
.
next
();
if
(!
springValue
.
getValue
().
isTargetBeanValid
())
{
// clear unused spring values
springValueIterator
.
remove
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment