使用google Guice 框架注入actor
GuiceActorProducer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
GuiceActorProducer
implements
IndirectActorProducer
{
private
final
Injector
injector
;
private
final
Class
<
Actor
>
actorType
;
public
GuiceActorProducer
(
Injector
injector
,
Class
<
Actor
>
actorType
)
{
this
.
injector
=
injector
;
this
.
actorType
=
actorType
;
}
@
Override
public
Actor
produce
(
)
{
return
injector
.
getInstance
(
actorType
)
;
}
@
Override
public
Class
<
?
extends
Actor
>
actorClass
(
)
{
return
actorType
;
}
}
|
GuiceExtension
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public
class
GuiceExtension
extends
AbstractExtensionId
<
GuiceExtension
.
GuiceExt
>
{
private
static
GuiceExtension
instance
=
new
GuiceExtension
(
)
;
@
Override
public
GuiceExt
createExtension
(
ExtendedActorSystem
extendedActorSystem
)
{
return
new
GuiceExt
(
)
;
}
public
class
GuiceExt
implements
Extension
{
private
volatile
Injector
injector
;
public
void
initialize
(
Injector
injector
)
{
this
.
injector
=
injector
;
}
public
Props
props
(
Type
actorType
)
{
return
Props
.
create
(
GuiceActorProducer
.
class
,
injector
,
actorType
)
;
}
}
public
static
GuiceExtension
getInstance
(
)
{
return
instance
;
}
}
|
GuiceModule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
public
class
GuiceModule
extends
AbstractModule
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
GuiceModule
.
class
)
;
private
static
final
String
ACTOR_SYSTEM
=
"event-evaluation"
;
private
static
Injector
injector
=
Guice
.
createInjector
(
new
GuiceModule
(
)
)
;
private
static
final
Config
config
=
ConfigFactory
.
load
(
)
;
private
static
final
int
ACTOR_POOLI_SIZE
=
config
.
getInt
(
"akka.actor.poolSize"
)
;
@
Override
protected
void
configure
(
)
{
bind
(
DataSource
.
class
)
.
annotatedWith
(
Names
.
named
(
"rdbDataSource"
)
)
.
toProvider
(
RDBDataSourceProvider
.
class
)
.
in
(
Singleton
.
class
)
;
bind
(
DataSource
.
class
)
.
annotatedWith
(
Names
.
named
(
"alertdbDataSource"
)
)
.
toProvider
(
AlertDBDataSourceProvider
.
class
)
.
in
(
Singleton
.
class
)
;
bind
(
EventSourceDataStore
.
class
)
.
to
(
EventSourceDataStoreImpl
.
class
)
.
in
(
Singleton
.
class
)
;
bind
(
DatapointAlertStore
.
class
)
.
to
(
DatapointAlertStoreImpl
.
class
)
.
in
(
Singleton
.
class
)
;
}
public
static
Injector
getInjector
(
)
{
return
injector
;
}
private
ActorRef
actorRef
(
ActorRef
supervisor
,
Props
props
)
{
try
{
return
(
ActorRef
)
Await
.
result
(
ask
(
supervisor
,
props
,
5000
)
,
Duration
.
create
(
"5 seconds"
)
)
;
}
catch
(
Exception
e
)
{
LOG
.
error
(
new
LogMessage
(
LogMessage
.
EventStatus
.
FAILURE
,
LogMessage
.
EventSeverity
.
CRITICAL
,
"configuration"
,
"Failure to initialize actors"
,
GuiceModule
.
class
.
toString
(
)
,
e
)
.
toString
(
)
)
;
throw
new
RuntimeException
(
"Failure to initialize actors"
,
e
)
;
}
}
@
Provides
public
ActorSystem
actorSystem
(
)
{
ActorSystem
actorSystem
=
ActorSystem
.
create
(
ACTOR_SYSTEM
)
;
GuiceExtension
.
getInstance
(
)
.
get
(
actorSystem
)
.
initialize
(
injector
)
;
return
actorSystem
;
}
@
Provides
@
Named
(
"supervisor"
)
public
ActorRef
supervisor
(
ActorSystem
actorSystem
)
{
return
actorSystem
.
actorOf
(
GuiceExtension
.
getInstance
(
)
.
get
(
actorSystem
)
.
props
(
Supervisor
.
class
)
,
"supervisor"
)
;
}
@
Provides
@
Named
(
"entryActorRef"
)
public
ActorRef
entryActor
(
ActorSystem
actorSystem
,
@
Named
(
"supervisor"
)
ActorRef
supervisor
)
{
Props
props
=
GuiceExtension
.
getInstance
(
)
.
get
(
actorSystem
)
.
props
(
EntryActor
.
class
)
;
return
actorRef
(
supervisor
,
props
)
;
}
}
|
考虑到actor 存在生命周期,因此不能是单例,即不应该对在ActorRef上加@Singleton
转载自IT豆子 http://www.itdouzi.com/java/134.html