title: How to create two configured instances of the same service?
body: <p>I have a service that interacts with a brokerage account's API.  It works fine, but now I need to interact with two different accounts at the same brokerage.</p>
  
  <p>It seems like the best way to handle this is to make it possible to configure the service to specify the target account and then instantiate two different instances, one for each account.</p>
  
  <p>I'm not sure if this is supported in Grails or how to go about it.</p>
  
  <p>Two questions:</p>
  
  <ol>
  <li>Is there a better way to do this?</li>
  <li>If not, how can I instantiate and configure two different service instances?</li>
  </ol>
  
  <p><strong>ADDITIONAL INFORMATION:</strong></p>
  
  <p>Both answers are near misses.  Let me try to clarify:</p>
  
  <p>I didn't want to get into the details, but it may help to explain what I'm after.  I'm using the Interactive Brokers trading API, and they don't let you talk directly to their servers the way other brokerages do.  You have to talk over a socket to their IB Gateway, which is a piece of software they provide that essentially proxies their servers.  So your app talks to IB Gateway, and IB Gateway talks to Interactive Brokers' servers on your app's behalf.</p>
  
  <p>The catch is that IB Gateway has to be logged in to an account as part of its configuration.  So, in order to trade two different accounts, you have no choice but to configure two different IB Gateways, since each can only access the account that it is configured for.</p>
  
  <p>So my Grails code for placing trades must select the right IB Gateway to talk to.  That means it needs to know the IP address and port of the IB Gateway that corresponds to each account.  Other than this setting for IP address and port, there is no difference between the two Grails services that communicate with IB Gateway.</p>
  
  <p>What I want is to reuse the same service class, each being instantiated as a singleton, simply having a different IP address and port on which to communicate.</p>
  
  <p>So making two different services is undesirable, since the code is otherwise identical.  (And if I add a third or fourth IB Gateway, this becomes fairly smelly code.)</p>
  
  <p>And this setting should exist for the life of the application, so I don't think a change in scope is really the answer, either.</p>
  
  <p>I really want two instances of the same service, simply having different configurations.</p>
  
  <p>I hope that helps explain the situation.  What do you suggest?  Thank you!</p>