last day (15 days later) » 

8:35 PM
0
Q: why does a constructor work this way?

rupwebI have a constructor in c#, could just as well be Java, for a class that uses activeMQ. The class is instantiated multiple times, each time to publish on a separate topic, as defined by the variable String destination passed to the constructor, as follows: class Publisher { // Class variables ...

 
That's basic OOP. The constructor is executed on an instance when it's created - any instance fields/properties apply only to that instance. On a side note - your constructor is doing WAY too much work. Constructors should only do the bare minimum to create a class. Any further actions should be done via methods (Init, Connect, Start, etc.)
 
@D Stanley, thx, so the only difference across 100 instances is the 1 property "destination" and that's enough for each instance to have unique pointer address when it's instantiated... so it can be found if called, and then everything else is all there in address space?
@D Stanley, hmm, constructor does too much, so then for each topic i want to publish to then make class instance, then init... hmm an extra line per topic...
 
@rupweb "destination" is not a property, it's a parameter to your constructor, which initializes the connection, session, and producer fields, each of which is part of that instance's state.
@rupweb a light constructor is a design principle. They should do as little as possible to give you a valid instance. Your constructor does set the instance fields, but also tries to connect to external resources. That action should be explicitly done in a separate method so the client can choose when to connect.
 
@D Stanley, thx. Guess my app has hundreds of instances of 'connection, session' and 'producer' objects all packed away in different memory blocks, each having an address according to 'destination'. Or, is each instance have its own pointer to some master 'connection, session' and 'producer' objects?
 
@rupweb Each instance will have it's own connection, session and producer objects. And they are isolated by instance, not by destination - meaning if you create another Publisher with the same destination parameter the two object will not share that state.
And having "hundreds" of objects is probably not a big deal - even if an object is 1MB in size (which is huge for a single object), you're still only at 100MB which isn't much on modern machines. Don't worry about memory usage until it actually becomes a problem.
 
8:35 PM
Thx, right, what's desirable is to setup the one connection, in the constructor, because all the sessions, each having discrete destinations and producers, use the same connection. If the connection doesn't complete nothing works. Then put all the session, destination and producer constructions in the Publish method, and build them up and tear them down with every Publish... Hmmm
hey
not seen a stackoverflow chat before... r u free?
guess the trade off between memory block usage, and constructing/destroying sessions each having destination/producer, is in favour of not doing the latter. if there's big data flow then too expensive to construct/destroy a session for each message on each topic..
better that each topic has their own session/destination/producer footprints and reused.
and who cares about the memory bill...
thats why i wanted put session/destination/producer in constructor for each instance, tho i take the point that the constructor is not exactly lightweight and maybe the session/destination/producer construction is done in a method, sounds much of a muchness, it all has to be done somewhere !
guess what i want is every destination (i.e. topic) to use the same session/producer as well as they all use the same connection, but i am not sure that's possible...
activeMQ
 
9:08 PM
I did as you suggest and it all still works
the constructor constructs the connection, without which the class doesnt work anyway, and the topics session/destination/producer are in a method to be called for each new topic...
that sounds good nuff for me
thx for your comments
 

last day (15 days later) »