15. Business Processor - Intro
Event
Business Processor
Input
Buffer
Évènements d'entrée
Thursday, June 27, 13
Business Logic
Output
Buffer
• Domain Model
• Logique métier
• Consumers
Évènements de sortie:
- persistence
- appels WS externes
16. Business Processor - Évènement d'entrée
@Immutable
public
class
RawDatapointEvent
{
private
final
String
sensorId;
private
final
DateTime
measureTime;
private
final
double
value;
public
RawDatapointEvent(String
sensorId,
DateTime
measureTime,
double
value)
{
this.sensorId
=
sensorId;
this.measureTime
=
measureTime;
this.value
=
value;
}
public
String
getSensorId()
{
return
sensorId;
}
public
DateTime
getMeasureTime()
{
return
measureTime;
}
public
double
getValue()
{
return
value;
}
}
Thursday, June 27, 13
17. Business Processor - Consumer
public
class
RawDatapointEventConsumer
implements
EventConsumer<RawDatapointEvent>
{
@Inject
DatapointPreprocessingService
preprocessingService;
@Override
public
void
consume(RawDatapointEvent
rawEvent,
Model
model,
EventProcessor
eventProcessor)
{
PreprocessingResult
result
=
preprocessingService.preprocess(model,
rawEvent);
if
(result.hasError())
{
eventProcessor.sendReply(new
FailedDatapointResult(result.getErrorMsg()));
}
eventProcessor.sendReply(new
SuccessfulDatapointResult());
eventProcessor.process(
new
AddDatapointToTimeSeriesEvent(result.getAccount(),
result.getSensor().getTimeSeries(),
result.getDatapoint()
)
);
}
}
Thursday, June 27, 13
18. Business Processor - Évènement de résultat
@Immutable
public
class
RawDatapointEvent
{
//
fields,
constructor,
getters...
public
interface
Result
{}
public
static
class
SuccessfulDatapointResult
implements
Result
{}
public
static
class
FailedDatapointResult
implements
Result
{
private
final
String
errorMessage;
public
FailedDatapointResult(String
errorMessage)
{
this.errorMessage
=
errorMessage;
}
public
String
getErrorMessage()
{
return
errorMessage;
}
}
}
Thursday, June 27, 13
19. Business Processor - Code client
RawDatapointEvent
rawEvent
=
createRawEventFromJson(httpRequest.getContent());
ListenableFuture<RawDatapointEvent.Result>
future
=
client.sendEvent(rawEvent);
Futures.addCallback(future,
new
FutureCallback<RawDatapointEvent.Result>()
{
@Override
public
void
onSuccess(RawDatapointEvent.Result
result)
{
...
}
@Override
public
void
onFailure(Throwable
t)
{
...
}
});
Thursday, June 27, 13
24. In-Memory Model - Data structures
CopyOnWriteArrayList
ConcurrentHashMap
ReentrantLock
HashSet
HashMap
ArrayList
Multimap
BiMap
Thursday, June 27, 13
25. In-Memory Model - Immutability
@Immutable
public
final
class
Money
{
//
I
swear,
those
are
final!
private
BigDecimal
amount;
private
Currency
currency;
//
For
Hibernate
only!
Money()
{}
public
Money(BigDecimal
amount,
Currency
currency)
{
this.amount
=
amount;
this.currency
=
currency;
}
public
BigDecimal
getAmount()
{
return
amount;
}
public
Currency
getCurrency()
{
return
currency;
}
//
For
Hibernate
only!
void
setAmount(BigDecimal
amount)
{
this.amount
=
amount;
}
void
setCurrency(Currency
currency)
{
this.currency
=
currency;
}
}
Thursday, June 27, 13
26. In-Memory Model - Immutability
@Immutable
public
final
class
Money
{
private
final
BigDecimal
amount;
private
final
Currency
currency;
public
Money(BigDecimal
amount,
Currency
currency)
{
this.amount
=
amount;
this.currency
=
currency;
}
public
BigDecimal
getAmount()
{
return
amount;
}
public
Currency
getCurrency()
{
return
currency;
}
}
Thursday, June 27, 13
27. In-Memory Model - Managers
public
class
UserManager
{
private
final
Map<String,
User>
users
=
Maps.newHashMap();
public
void
add(@Nonnull
User
user)
{
users.put(user.primaryKey(),
user);
}
public
void
remove(@Nonnull
String
userId)
{
User
removedUser
=
users.remove(userId);
if
(removedUser
==
null)
throw
new
UserNotFoundException(userId);
}
public
User
getById(@Nonnull
String
userId)
{
User
user
=
users.get(userId);
if
(user
==
null)
throw
new
UserNotFoundException(userId);
return
user;
}
public
List<User>
getAll()
{
return
ImmutableList.copyOf(users.values());
}
}
Thursday, June 27, 13
28. In-Memory Model - Memory Size
http://commons.wikimedia.org/wiki/File:Hard_disk_Western_Digital_WD740_1_(dark1).jpg
Thursday, June 27, 13