application setup (simplified)
i feel pretty familiar concurrency in java, relatively new java ee , annotations.
i have class called datahandler
annotated @singleton
performs business logic application. class injected web service implementation class annotated @webservice
. when 1 of web service's methods called, datahandler
calls parsefiles()
method parses files, , stores data inside of datahandler
. afterwards makes calls getters inside of datahandler
:
@webservice(portname = "portname", servicename = "myservice", endpointinterface = "com.myco.myporttype", wsdllocation = "web-inf/wsdl/mywsdl.wsdl", targetnamespace = "targetnamespace") public class myserviceimpl implements myporttype { @ejb private datahandler datahandler; @override public responsetype dostuff(requesttype request) { // code // .... datahandler.parsefiles(); // .... // more code map<string, customtype> map = datahandler.getcustomtypemap(); map<string, customtype2> map2 = datahandler.getcustomtype2map(); }
now have message-driven bean receives data on topic called updatetopic, converts type , stores inside of datahandler
:
@messagedriven(activationconfig = { @activationconfigproperty(propertyname = "acknowledgemode", propertyvalue = "auto-acknowledge"), @activationconfigproperty(propertyname = "destinationtype", propertyvalue = "javax.jms.topic"), @activationconfigproperty(propertyname = "destination", propertyvalue = "updatetopic") }) public class updatetopicmdb implements messagelistener { @ejb datahandler datahandler; @override public void onmessage(message message) { try { if (message instanceof objectmessage) { objectmessage objmessage = (objectmessage) message; serializable data = objmessage.getobject(); if (data instanceof updatetype) { datahandler.convertandstore((updatetype) data); } } } catch(exception e) { // log exception } }
i wanted synchronize parsefiles()
method, convertandstore()
method, , 2 getters data up-to-date when accessed.
i made 4 methods synchronized
achieve this. understanding if files in middle of being parsed , update comes mdb, convertandstore()
method locked until parsefiles()
done. likewise, if update being converted , stored , web service trying access 1 of 2 maps, wait until convertandstore()
finished. correct me if i'm wrong on of that.
my question
with of being said, read @concurrencymanagement
, @lock
annotations. instead of synchronizing these 4 methods. seems though should annotate parsefiles()
, convertandstore()
@lock(locktype.write)
since modifying bean's state , 2 getters @lock(locktype.read)
.
should , avoid using synchronized
keyword altogether? there use synchronized
(maybe synchronized blocks?), or these annotations replace now?
when code javaee, should avoid synchronized
, other manual concurrency implementation. not create threads , locks. instead of use jsr 236 (ee concurrency utilities).
Comments
Post a Comment