Skip to content

Instantly share code, notes, and snippets.

@viniciusccarvalho
Last active December 14, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viniciusccarvalho/5014818 to your computer and use it in GitHub Desktop.
Save viniciusccarvalho/5014818 to your computer and use it in GitHub Desktop.
Actor dilema
def start(){
Actors.defaultActorPGroup.resize(props.get("threadCount") as Integer)
def file = new File(props.get("diffFile.path") as String)
final HttpActor httpActor = new HttpActor().start()
final XMLActor xmlActor = new XMLActor(httpActor:httpActor).start()
AtomicInteger counter = new AtomicInteger()
def Integer min = props.get("catalog.min") == null ? Integer.MIN_VALUE : props.get("catalog.min") as Integer;
def Integer max = props.get("catalog.max") == null ? Integer.MAX_VALUE : props.get("catalog.max") as Integer;
xmlActor.metaClass.afterStop = {
httpActor.stop()
}
file.eachLine { line ->
def chunks = line.split(",")
def id = chunks[0].replaceAll("\\\"","").trim()
def name = chunks[1].replaceAll("\\\"","").trim()
if((id as Integer) > min && (id as Integer) < max){
Actors.actor{
xmlActor << new FileToRead(basePath:props.get("source.path") as String,id:id,name:name, fileCounter:counter)
}
}
}
xmlActor.stop()
[httpActor, xmlActor]*.join()
println "Shutting down"
}
//XMLActor
public void onMessage(FileToRead message) {
def File file = new File("${message.basePath}/${message.id.substring(0,3)}/${message.id.substring(3,6)}/${message.id}/${message.name}")
log.debug "Reading contents from ${file.getAbsolutePath()}"
def countries = []
def territories = []
def a1 = new XmlSlurper().parse(file)
def checkUnpublished = AlbumPriceComparator.props.get("checkUnpublished") == null ? false : AlbumPriceComparator.props.get("checkUnpublished") as Boolean;
if(a1.published == true || checkUnpublished){
territories = []
territories = a1.territorialAvailability
if(territories.size() == 0){
countries.add("US")
}else{
territories.each {
if(it.@territory.toString().equalsIgnoreCase("world"))
countries.add("US")
else
countries.add(it.@territory)
}
}
log.debug "Calling httpActors for the following countries ${countries}"
countries.each { country ->
Actors.actor{
httpActor << new AlbumPriceMessage(id:message.id, country:country)
}
}
}
}
public void onMessage(AlbumPriceMessage message) {
try {
log.debug "Contacting pricing servers for [id:${message.id}, country: ${message.country}]"
def HttpURLClient basePriceServer = new HttpURLClient(url: "")
def HttpURLClient comparePriceServer = new HttpURLClient(url: "")
// lots of comparison happens here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment