I am implementing a webservice witch is used to attack one DB.
i need to generate ID for objects that i store and i don't know what's the best way to do it.
i need to increment a INT.
Obviously the webservice must to be used for so much people and maybe various at same time.
so, what's is a good solution?
singleton/synchronize??
i think is the only way i know, maybe there are others better.
if u can show me one example it will be very appreciated.
thanks in advance!
Synchronize has horrible overhead. If all you need is an incremental counter, you can use AtomicLong's incrementAndGet(). Put the AtomicLong in a Singleton to have a server-wide access.
Edit: Some code example:
import java.util.concurrent.atomic.AtomicLong;
public class AtomicIdGenerator
{
private static class SingletonHolder
{
public static final AtomicIdGenerator instance = new AtomicIdGenerator();
}
public static AtomicIdGenerator getInstance()
{
return SingletonHolder.instance;
}
private AtomicLong mIdGenerator = null;
private AtomicIdGenerator()
{
mIdGenerator = new AtomicLong();
}
private AtomicLong getGenerator()
{
return mIdGenerator;
}
public long getNewId()
{
return getGenerator().incrementAndGet();
}
}
Usage example is simply:
long tNewId = AtomicIdGenerator.getInstance().getNewId();
This will be thread-safe, and without any overhead from synchronization. If you foresee yourself handling lots of concurrent use cases in the future, the java.util.concurrent package provides lots of battle-proven implementations for your use cases.
Use synchronize block to achieve this. In synchronized block only one thread can enter inside it.
JVM guarantees that Java synchronized code will only be executed by one thread at a time.
You can do something like this. I've done it a while back, it was based on PostgreSql and iBatis, but you can get the idea.
public class Sequence implements Serializable {
private static final long serialVersionUID = 7526471155622776147L;
private String name = null;
private int nextId = 0;
public Sequence () {
}
public Sequence (String name, int nextId) {
this.name = name;
this.nextId = nextId;
}
public final String getName () {
return name;
}
public final void setName (String name) {
this.name = name;
}
public final int getNextId () {
return nextId;
}
public final void setNextId (int nextId) {
this.nextId = nextId;
}
}
public class SequenceSqlMapDao extends SqlMapClientDaoSupport implements SequenceDao {
/**
* This is a generic sequence ID generator that is based on a database
* table called 'SEQUENCE', which contains two columns (NAME, NEXTID).
* <p/>
* This approach should work with any database.
*
* #param name The name of the sequence.
* #return The Next ID
* #
*/
public final synchronized int getNextId(String name) {
Sequence sequence = new Sequence(name, -1);
//Sequence sequence = new Sequence();
sequence = (Sequence) getSqlMapClientTemplate ().queryForObject("getSequence", sequence);
if (sequence == null) {
try {
throw new IllegalArgumentException("Error: SHOOT! A null sequence was returned from the database (could not get next " + name + " sequence).");
} catch (Exception ex) {
Logger.getLogger(SequenceSqlMapDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
Object parameterObject = new Sequence(name, sequence.getNextId() + 1);
getSqlMapClientTemplate ().update("updateSequence", parameterObject);
int nextId = sequence.getNextId();
parameterObject = null;
sequence = null;
return nextId;
}
}
If nothing else this is pretty database agnostic. You'd still have to expose the method in your webservice.
PS - I forgot where I got this from, otherwise I'd give credit to proper source.
Related
To keep things as simple as I can, I am trying to pass an ArrayList holding values used for a method in a separate class in my program. The issue is that whatever I tweak, the values held in retailerID and prodCat never seem to make it to the subscribe method in the other class as previously mentioned. Here is the snippet I am trying to overcome at the moment. Please ingore the commented out portions as those were previous attempts I've yet to get rid of.
public class SupplyDemand {
Producer nProducer = new Producer();
Retailer nRetailer = new Retailer();
Broker nBroker = new Broker();
ArrayList<String> output = new ArrayList<String>();
ArrayList<String> inputCommand = new ArrayList<String>();
/**
* Class constructor - you may set up any needed objects and data here. Specification of constructor is optional - it is acceptable if you leave it blank.
*/
public SupplyDemand() {
}
/**
* This method accepts a single command and carry out the instruction given. You do not need to (and probably shouldn't) do everything in this method - delegate responsibilities to other classes.
*/
public void processInput(String command) {
//command.toLowerCase();
//String str[] = command.split(",");
inputCommand.add(command.toLowerCase());
//inputCommand = Arrays.asList(str);
if(inputCommand.contains("subscribe")){
String retailerID = inputCommand.get(1);
String prodCat = inputCommand.get(2);
nRetailer.subscribe(retailerID, prodCat);
}
else if(inputCommand.contains("unsubscribe")){
String retailerID = inputCommand.get(1);
String prodCat = inputCommand.get(2);
nRetailer.unsubscribe(retailerID, prodCat);
}
else if(inputCommand.contains("publish")){
String producerID = inputCommand.get(1);
String prodCat = inputCommand.get(2);
String brand = inputCommand.get(3);
nProducer.publish(brand, prodCat, producerID);
nBroker.checkMatch();
}
else{
}
//nBroker.checkMatch();
}
/**
* After each round of execution, this method would be called to fetch all output lines, if there are any. The lines must be ordered by the time they are received.
*/
public ArrayList<String> getAggregatedOutput() {
output = nRetailer.subscribeList;
return output;
}
/**
* Finally, this method would be called to clear all saved information in the system, so that information from previous round would not be carried to next round. After calling this method the system should be effectively starting anew.
*/
public void reset() {
nRetailer.subscribeList.clear();
nProducer.producerList.clear();
nBroker.buildMatch.clear();
}
}
Here is the Retailer class with the subscribe method and how I need to use the needed strings if its any help.
public class Retailer implements ISubscriber {
public String retailerID = " ";
public ArrayList<String> subscribeList = new ArrayList<String>();
/*/**
* #see SupplyDemand.ISubscriber#subscribe(String)
*/
public void subscribe(String retailerID, String prodCat) {
subscribeList.add("TEST1");
if(retailerID.equals(" ")){
subscribeList.add(retailerID);
subscribeList.add(prodCat);
}
else{
subscribeList.add("TEST2");
}
}
/*/**
* #see SupplyDemand.ISubscriber#unsubscribe(String)
*/
public void unsubscribe(String retailerID, String prodCat) {
int removeRetailer = subscribeList.indexOf(retailerID);
int removeProdCat = subscribeList.indexOf(removeRetailer + 1);
subscribeList.remove(removeProdCat);
subscribeList.remove(removeRetailer);
}
}
I am trying to remove the limit, through the default setting, when adding new numbers. I wish to still have a limit on the all-arg constructor, that is set when initialized, but not on the no-arg and 1-arg constructors.
public class SMSDataModelList implements SMSDataModelInterface, Serializable {
List<String> list = new ArrayList<String>();
//List<String> list = new LinkedList<String>();
private static final long serialVersionUID = 1L;
private static final int DEFAULT_MAX_NUM_PHONE_NUMBERS = 20;
public static final String FULL = "FULL";
public static final String DUPLICATE = "DUPLICATE";
private String message; //The SMS message
private String[] phoneNumbers; //The collection of phone numbers
private int maxNumPhoneNumbers; //Max numbers in list
public SMSDataModelList(String message) {
this(message, DEFAULT_MAX_NUM_PHONE_NUMBERS);
}
public SMSDataModelList() {
this("", DEFAULT_MAX_NUM_PHONE_NUMBERS);
}
public SMSDataModelList(String initialMessage, int maxNumPhoneNumbers) {
this.message = initialMessage;
this.maxNumPhoneNumbers = maxNumPhoneNumbers;
this.phoneNumbers = new String[maxNumPhoneNumbers];
}
#Override
public String addPhoneNumber(String newPhoneNumber) {
String result;
if (list.size() == maxNumPhoneNumbers) {
result = FULL;
} else {
boolean exists;
exists = findPhoneNumberIndex(newPhoneNumber) != -1;
if (exists) {
result = DUPLICATE;
} else {
list.add(newPhoneNumber);
result = newPhoneNumber;
}
}
return result;
}
}
I have tried adding this to the addPhoneNumber method:
if (DEFAULT_MAX_NUM_PHONE_NUMBERS == 20) {
maxNumPhoneNumbers = (list.size() + 1);
}
I thought this was working well with testing the no-arg and 1-arg constructors, but when it came to the all-arg, I noticed it allowed the array size to increase there too. I can see why that was wrong now, but not sure if I was on the right track.
Is it actually possible for me to make the no-arg and 1-arg constructors have unlimited array sizes with a static default?
If I understand what you mean, it seems you require another flag in your constructors, to indicate whether the number of phones should be limited to the initial maximum number:
public SMSDataModelList(String message) {
this(message, DEFAULT_MAX_NUM_PHONE_NUMBERS, false);
}
public SMSDataModelList() {
this("", DEFAULT_MAX_NUM_PHONE_NUMBERS, false);
}
public SMSDataModelList(String initialMessage, int maxNumPhoneNumbers) {
this(initialMessage, maxNumPhoneNumbers, true);
}
public SMSDataModelList(String initialMessage, int maxNumPhoneNumbers, boolean limited) {
this.message = initialMessage;
this.maxNumPhoneNumbers = maxNumPhoneNumbers;
this.phoneNumbers = new String[maxNumPhoneNumbers];
this.limited = limited;
}
You can use the limited flag to determine whether the phoneNumbers array can be increased when it becomes full - you'll need to add logic to addPhoneNumber that checks that flag.
P.S. I just noticed that your constructors and instance variables don't match the addPhoneNumber method. The constructors initialize an array to store the phone numbers while the addPhoneNumber method uses an ArrayList. You should decide which of the two you wish to use. An ArrayList would be easier, since you won't have to re-allocate a new array each time the existing array becomes full.
I just began with Apache Storm. I read the tutorial and had a look into examples My problem is that all example work with very simple tuples (often one filed with a string). The tuples are created inline (using new Values(...)). In my case i have tuples with many fields (5..100). So my question is how to implement such tuple with name and type (all primitive) for each field?
Are there any examples? (i think directly implementing "Tuple" isn't a good idea)
thanks
An alternative to creating the tuple with all of the fields as a value is to just create a bean and pass that inside the tuple.
Given the following class:
public class DataBean implements Serializable {
private static final long serialVersionUID = 1L;
// add more properties as necessary
int id;
String word;
public DataBean(int id, String word) {
setId(id);
setWord(word);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
Create and emit the DataBean in one bolt:
collector.emit(new Values(bean));
Get the DataBean in the destination bolt:
#Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
try {
DataBean bean = (DataBean)tuple.getValue(0);
// do your bolt processing with the bean
} catch (Exception e) {
LOG.error("WordCountBolt error", e);
collector.reportError(e);
}
}
Don't forget to make your bean serializable and register when you set up your topology:
Config stormConfig = new Config();
stormConfig.registerSerialization(DataBean.class);
// more stuff
StormSubmitter.submitTopology("MyTopologyName", stormConfig, builder.createTopology());
Disclaimer: Beans will work fine for shuffle grouping. If you need to do a fieldsGrouping, you should still use a primitive. For example, in the Word Count scenario, you need go group by word so you might emit:
collector.emit(new Values(word, bean));
I would implement a custom tuple/value type as follows: Instead of using member variables to store the data, each attribute is mapped to a fixed index into the object list of the inherited Values types. This approach avoids the "field grouping" problem a regular Bean.
it in not required to add additional attributes for fields grouping (what is quite unnatural)
data duplication is avoided (reducing the number of shipped bytes)
it preserves the advantage of the beans pattern
An example for word count example would be something like this:
public class WordCountTuple extends Values {
private final static long serialVersionUID = -4386109322233754497L;
// attribute indexes
/** The index of the word attribute. */
public final static int WRD_IDX = 0;
/** The index of the count attribute. */
public final static int CNT_IDX = 1;
// attribute names
/** The name of the word attribute. */
public final static String WRD_ATT = "word";
/** The name of the count attribute. */
public final static String CNT_ATT = "count";
// required for serialization
public WordCountTuple() {}
public WordCountTuple(String word, int count) {
super.add(WRD_IDX, word);
super.add(CNT_IDX, count);
}
public String getWord() {
return (String)super.get(WRD_IDX);
}
public void setWort(String word) {
super.set(WRD_IDX, word);
}
public int getCount() {
return (Integer)super.get(CNT_IDX);
}
public void setCount(int count) {
super.set(CNT_IDX, count);
}
public static Fields getSchema() {
return new Fields(WRD_ATT, CNT_ATT);
}
}
To avoid inconsistencies, final static variables for "word" and "count" attribute are used. Furthermore, a method getSchema() returns the implemented schema to be used to declare output streams in Spout/Bolt method .declareOutputFields(...)
For output tuples, this type can be used straight forward:
public MyOutBolt implements IRichBolt {
#Override
public void execute(Tuple tuple) {
// some more processing
String word = ...
int cnt = ...
collector.emit(new WordCountTuple(word, cnt));
}
#Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(WordCountTuple.getSchema());
}
// other methods omitted
}
For input tuples, I would suggest the following pattern:
public MyInBolt implements IRichBolt {
// use a single instance for avoid GC trashing
private final WordCountTuple input = new WordCountTuple();
#Override
public void execute(Tuple tuple) {
this.input.clear();
this.input.addAll(tuple.getValues());
String word = input.getWord();
int count = input.getCount();
// do further processing
}
// other methods omitted
}
MyOutBolt and MyInBolt can be connected as follows:
TopologyBuilder b = ...
b.setBolt("out", new MyOutBolt());
b.setBolt("in", new MyInBolt()).fieldsGrouping("out", WordCountTuple.WRD_ATT);
Using fields grouping is straight forward, because WordCountTuple allows to access each attribute individually.
I have recently learned Joshua Bloch's builder pattern for creating objects with many optional fields. I've been using something like it for years, but never used an inner-class until Bloch's book suggested it to me. I love it.
I understand that another thread may alter the bulider's configuration, before it's actually built (with build()), such that it may be necessary to re-validate all values in the constructor of the enclosing class. Below is an example of a builder class that optionally reverifies its data.
So my question is this: Assuming this is a robust enough design, when there are defaults for all values--knowing this class is a poor choice for using defaults--and when every set-attempt is validated, is this re-check necessary? Although it may be different, it would never be invalid. Is that correct?
(Although this design is manageable, it is certainly complicated by the potential need for re-verification. And, honestly, I never multi-thread, but I don't want to make my library unusable by people that do.)
Thank you for any advice.
/**
<P><CODE>java ReverifyBuilderInEnclosingCnstrXmpl</CODE></P>
**/
public class ReverifyBuilderInEnclosingCnstrXmpl {
public static final void main(String[] igno_red) {
//Don't reverify
ReverifyBuilderInEnclosingCnstrXmpl rvbdx = new ReverifyBuilderInEnclosingCnstrXmpl.Cfg().
name("Big Bird").age(6).build();
System.out.println(rvbdx.sName);
System.out.println(rvbdx.iAge);
//Do reverify
rvbdx = new ReverifyBuilderInEnclosingCnstrXmpl.Cfg().
reverifyInEnclosing().
name("Big Bird").age(6).build();
}
public final String sName;
public final int iAge;
/**
<P>Create a new <CODE>ReverifyBuilderInEnclosingCnstrXmpl</CODE> with defaults.</P>
**/
public ReverifyBuilderInEnclosingCnstrXmpl() {
//Does not reverify. No need.
this(new ReverifyBuilderInEnclosingCnstrXmpl.Cfg());
}
private ReverifyBuilderInEnclosingCnstrXmpl(ReverifyBuilderInEnclosingCnstrXmpl.Cfg rbdx_c) {
sName = rbdx_c.sName;
iAge = rbdx_c.iAge;
ReverifyBuilderInEnclosingCnstrXmpl.Cfg.zcibValues(rbdx_c, sName, iAge, "constructor");
}
public static class Cfg {
private String sName = null;
private int iAge = -1;
private boolean bReVrfy = false;
public Cfg() {
//Defaults
bReVrfy = false;
name("Broom Hilda");
age(127);
}
//Self-returning configuration...START
//No way to unset.
public Cfg reverifyInEnclosing() {
bReVrfy = true;
return this;
}
public Cfg name(String s_name) {
zcib_name(s_name, "name");
sName = s_name;
return this;
}
public Cfg age(int i_age) {
zcib_age(i_age, "age");
iAge = i_age;
return this;
}
//Self-returning configuration...END
//Validate config...START
public static final void zcibValues(ReverifyBuilderInEnclosingCnstrXmpl.Cfg rbdx_c, String s_name, int i_age, String s_clgFunc) {
try {
if(!rbdx_c.bReVrfy) {
return;
}
} catch(NullPointerException npx) {
throw new NullPointerException("zcibValues: rbdx_c");
}
zcib_name(s_name, s_clgFunc);
zcib_age(i_age, s_clgFunc);
}
public static final void zcib_name(String s_name, String s_clgFunc) {
if(s_name == null || s_name.length() == 0) {
throw new IllegalArgumentException(s_clgFunc + ": s_name (" + s_name + ") is null or empty.");
}
}
public static final void zcib_age(int i_age, String s_clgFunc) {
if(i_age < 0) {
throw new IllegalArgumentException(s_clgFunc + ": i_age (" + i_age + ") is negative.");
}
}
//Validate config...END
public ReverifyBuilderInEnclosingCnstrXmpl build() {
return (new ReverifyBuilderInEnclosingCnstrXmpl(this));
}
}
}
Firstly - the builder pattern is not inherently thread unsafe. I am not sure how you are concluding that it is. Each thread that intends to use the builder will create its own Builder object, populate it in Joshua Bloch's pragmatic and beautiful way and use it to construct the object. There are no static variables being affected anywhere in that mechanism, there is no thread unsafety unless you introduce it yourself.
Your concern about validation is - in my humble opinion - a gross pre-optimisation that produces hideously contrived and horribly bloated code. There is no reason to try to avoid validation just because you know the data is valid. Validation is almost always trivial and often takes little more that a few instructions. By bloating the class with these horrible static validation methods you are probably adding thousands of times more cpu cycles just to load this bloated code than you are saving by avoiding the validation.
Compare your contrived and bloated code with this lucid, succinct and patently correct and thread safe code and see what I mean:
public class Thing {
public final String name;
public final int age;
public Thing() {
this(new Thing.Builder());
}
private Thing(Thing.Builder builder) {
name = builder.name;
age = builder.age;
}
public static class Builder {
private String name = null;
private int age = -1;
public Builder() {
name("Broom Hilda");
age(127);
}
public Builder name(String name) {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException("Thing.Builder.name (" + name + ") is null or empty.");
}
this.name = name;
return this;
}
public Builder age(int age) {
if (age < 0) {
throw new IllegalArgumentException("Thing.Builder.age (" + age + ") is negative.");
}
this.age = age;
return this;
}
public Thing build() {
return (new Thing(this));
}
}
}
You are misunderstanding the pattern on an architectural level: all data during construction is tied to the local thread and not to be exposed to any external handler. The very moment build is called, the now finalized set of parameters is passed to an immutable object, which then first should verify the validity of those parameters in the constructor, then either return the final object or throw an exception.
As long as you keep the builder parameters thread-local, you cannot cause any threading-issues. If you violate this rule, you should ask yourself if what you are doing is correct and/or how you could solve it in a more fine-grained way.
So if you in your example need to use the builder from different threads, the simplest and safest way is to create a new builder instance instead of doing it statically. If you worry about performance, ThreadLocal is your friend.
I read this code in Thinking in Java and get puzzled:
package generics;
//: generics/Mixins.java
import java.util.*;
interface TimeStamped { long getStamp(); }
class TimeStampedImp implements TimeStamped {
private final long timeStamp;
public TimeStampedImp() {
timeStamp = new Date().getTime();
}
public long getStamp() { return timeStamp; }
}
interface SerialNumbered { long getSerialNumber(); }
class SerialNumberedImp implements SerialNumbered {
private static long counter = 1;
private final long serialNumber = counter++;
public long getSerialNumber() { return serialNumber; }
}
interface Basic {
public void set(String val);
public String get();
}
class BasicImp implements Basic {
private String value;
public void set(String val) { value = val; }
public String get() { return value; }
}
class Mixin extends BasicImp
implements TimeStamped, SerialNumbered {
private TimeStamped timeStamp = new TimeStampedImp();
private SerialNumbered serialNumber =
new SerialNumberedImp();
public long getStamp() { return timeStamp.getStamp(); }
public long getSerialNumber() {
return serialNumber.getSerialNumber();
}
}
public class Mixins {
public static void main(String[] args) {
Mixin mixin1 = new Mixin(), mixin2 = new Mixin();
mixin1.set("test string 1");
mixin2.set("test string 2");
System.out.println(mixin1.get() + " " +
mixin1.getStamp() + " " + mixin1.getSerialNumber());
System.out.println(mixin2.get() + " " +
mixin2.getStamp() + " " + mixin2.getSerialNumber());
while(true)System.out.println(new Date().getTime());
}
} /* Output: (Sample)
test string 1 1132437151359 1
test string 2 1132437151359 2
*///:~
Why are the values returned of getStamp() the same? (1132437151359 == 1132437151359)?
Two objects are created and they have different propoties created in different time, so Why?
The expression new Date().getTime() is a slow way of doing System.currentTimeMillis() which has a minimum resolution of one milli-seconds (but can be as much as 16 ms on some OSes)
This means if the method is called less than one milli-second apart it can give the same result.
A better option is to use AtomicLong.getAndIncrement() for ids.
Using time for serial numbers is not a good idea. The reason you're getting the same time is probably because the code runs rather quickly and enough time doesn't elapse between instantiation of the first object and the second. The time stamp is returned in milliseconds and so if the instantiation of both objects is within 1ms of each other, you won't see a difference.
If you increase load on the system, you might see a difference, or if you use Thread.sleep(5) to cause your program to pause. Both approaches aren't very good.
Instead of using the time for a unique id, use UUID.
Try something like this:
Mixin mixin1 = new Mixin();
Thread.sleep(10);
Mixin mixin2 = new Mixin();
Now you got 10 ms pause in the process of creating those 2 objects.
Your class is simple and you have fast computer so distance in time between two instantations is so small that Java can't see it.