You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hivemind.apache.org by Viktor Szathmary <ph...@gmail.com> on 2005/05/09 22:01:24 UTC

referencing "self" in a service

hi,

how can a hivemind-managed object refer to it's "real self"? for
example, if I need to know the service ID, or want to be able to pass
the proxied instance from the service itself to another object...

btw, i have briefly tried beta-1 (instead of alpha-3), and it seems
service serialization broke - it keeps complaining about "instance not
created in this VM".. since i didnt have time to focus on this, i just
rolled back to alpha-3...

regards,
  viktor

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Pablo Lalloni <pl...@afip.gov.ar>.
That's exactly the solution I've used in the past...  and it's working 
flawlessly, being the only disadvantage the need for a static to get it 
working.

But I think this is acceptable for an application. Not for HiveMind itself 
though.

This makes me think that HiveMind's current (1.1-beta-1) solution to service 
serialization is out of place since it's an application level decision how 
the registry is obtained (a static, jndi, whatever).

So I think hivemind could just detect when a service is Serializable and then 
in the proxy the methods corresponding to java serialization protocol that 
the service implementation has. Of course, leaving the current support as an 
optional default in some way could be nice, but I can not see how to do that 
in this moment.

El Mar 10 May 2005 00:06, Viktor Szathmary escribió:
> I went with the service-id injection and implementing a
> readresolve/writereplace myself, just because i needed the service-id
> anyway, and found it more natural to use "this" instead of getSelf() -
> so I ended up with a service base class similar to this:
>
> public abstract class AbstractFooService implements FooServiceI,
> Serializable {
>
> 	private String name;
>
> 	public String getName() {
> 		return name;
> 	}
>
> 	public void setName(String name) {
> 		this.name = name;
> 	}
>
> 	protected Object writeReplace() {
> 		return new ServiceRef(getName());
> 	}
>
> 	private static class ServiceRef implements Serializable {
> 		private final String name;
>
> 		public ServiceRef(String name) {
> 			this.name = name;
> 		}
>
> 		protected Object readResolve() throws ObjectStreamException {
> 			Object service = MyRegistry.getInstance().getService(name,
> FooServiceI.class);
> 			if (service == null) {
> 				throw new InvalidObjectException("Unable to find in FDRegistry: " +
> name); }
> 			return service;
> 		}
>
> 	}
> }
>
> On 5/9/05, James Carman <ja...@carmanconsulting.com> wrote:
> > Yes, basically it'd be a service that's dependent upon itself...
> >
> > public class MyService implements MyServiceI
> > {
> >   private MyServiceI self;
> >
> >   public void setSelf( MyServiceI self )
> >   {
> >     this.self = self;
> >   }
> >
> >   public Foo findObject()
> >   {
> >     return new Foo( self );
> >   }
> > }
> >
> > -----Original Message-----
> > From: Howard Lewis Ship [mailto:hlship@gmail.com]
> > Sent: Monday, May 09, 2005 7:00 PM
> > To: hivemind-user@jakarta.apache.org; Viktor Szathmary
> > Subject: Re: referencing "self" in a service
> >
> > BTW ... you can have HiveMind inject the service proxy into the service
> > implementation, just like any other collaborating service.
> >
> > On 5/9/05, Viktor Szathmary <ph...@gmail.com> wrote:
> > > On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> > > > Or better:
> > > >
> > > >  <service-point id="MyService" interface="com.example.MyServiceI">
> > > >         <invoke-factory>
> > > >                 <construct class="com.example.MyService"
> >
> > service-id-property="aProperty"/>
> >
> > > >         </invoke-factory>
> > > >  </service-point>
> > >
> > > thanks, injecting the service-id works well for me. (and it is cool :)
> > >
> > > > Now we have support natively in HiveMind 1.1 for serialization with
> > > > a similar approach but you have to call registry.setupThread()
> > > > before the functionality can be used on a given thread.
> > >
> > > seems like it worked fine without setupThread in alpha-2 (but not in
> > > beta-1)... but I suspect the reason for this change was this: "Allow
> > > for running with multiple Registries. (HLS) Fixes HIVEMIND-83"
> > >
> > > thanks,
> > >  viktor
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> >
> > --
> > Howard M. Lewis Ship
> > Independent J2EE / Open-Source Java Consultant
> > Creator, Jakarta Tapestry
> > Creator, Jakarta HiveMind
> >
> > Professional Tapestry training, mentoring, support
> > and project work.  http://howardlewisship.com
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org

-- 
Pablo I. Lalloni <pl...@afip.gov.ar>
Teléfono +54 (11) 4347-3177 
Proyecto Pampa
Dirección Informática Tributaria
AFIP

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We are each entitled to our own opinion, but no one is entitled to his
own facts.
		-- Patrick Moynihan

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Viktor Szathmary <ph...@gmail.com>.
I went with the service-id injection and implementing a
readresolve/writereplace myself, just because i needed the service-id
anyway, and found it more natural to use "this" instead of getSelf() -
so I ended up with a service base class similar to this:

public abstract class AbstractFooService implements FooServiceI, Serializable {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	protected Object writeReplace() {
		return new ServiceRef(getName());
	}

	private static class ServiceRef implements Serializable {
		private final String name;

		public ServiceRef(String name) {
			this.name = name;
		}

		protected Object readResolve() throws ObjectStreamException {
			Object service = MyRegistry.getInstance().getService(name,
FooServiceI.class);
			if (service == null) {
				throw new InvalidObjectException("Unable to find in FDRegistry: " + name);
			}
			return service;
		}

	}
}

On 5/9/05, James Carman <ja...@carmanconsulting.com> wrote:
> Yes, basically it'd be a service that's dependent upon itself...
> 
> public class MyService implements MyServiceI
> {
>   private MyServiceI self;
> 
>   public void setSelf( MyServiceI self )
>   {
>     this.self = self;
>   }
> 
>   public Foo findObject()
>   {
>     return new Foo( self );
>   }
> }
> 
> -----Original Message-----
> From: Howard Lewis Ship [mailto:hlship@gmail.com]
> Sent: Monday, May 09, 2005 7:00 PM
> To: hivemind-user@jakarta.apache.org; Viktor Szathmary
> Subject: Re: referencing "self" in a service
> 
> BTW ... you can have HiveMind inject the service proxy into the service
> implementation, just like any other collaborating service.
> 
> On 5/9/05, Viktor Szathmary <ph...@gmail.com> wrote:
> > On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> > >
> > > Or better:
> > >
> > >  <service-point id="MyService" interface="com.example.MyServiceI">
> > >         <invoke-factory>
> > >                 <construct class="com.example.MyService"
> service-id-property="aProperty"/>
> > >         </invoke-factory>
> > >  </service-point>
> > >
> >
> > thanks, injecting the service-id works well for me. (and it is cool :)
> >
> > > Now we have support natively in HiveMind 1.1 for serialization with
> > > a similar approach but you have to call registry.setupThread()
> > > before the functionality can be used on a given thread.
> >
> > seems like it worked fine without setupThread in alpha-2 (but not in
> > beta-1)... but I suspect the reason for this change was this: "Allow
> > for running with multiple Registries. (HLS) Fixes HIVEMIND-83"
> >
> > thanks,
> >  viktor
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> >
> >
> 
> --
> Howard M. Lewis Ship
> Independent J2EE / Open-Source Java Consultant
> Creator, Jakarta Tapestry
> Creator, Jakarta HiveMind
> 
> Professional Tapestry training, mentoring, support
> and project work.  http://howardlewisship.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


RE: referencing "self" in a service

Posted by James Carman <ja...@carmanconsulting.com>.
Yes, basically it'd be a service that's dependent upon itself...

public class MyService implements MyServiceI
{
  private MyServiceI self;

  public void setSelf( MyServiceI self )
  {
    this.self = self;
  }

  public Foo findObject()
  {
    return new Foo( self );
  }
}

-----Original Message-----
From: Howard Lewis Ship [mailto:hlship@gmail.com] 
Sent: Monday, May 09, 2005 7:00 PM
To: hivemind-user@jakarta.apache.org; Viktor Szathmary
Subject: Re: referencing "self" in a service


BTW ... you can have HiveMind inject the service proxy into the service
implementation, just like any other collaborating service.

On 5/9/05, Viktor Szathmary <ph...@gmail.com> wrote:
> On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> >
> > Or better:
> >
> >  <service-point id="MyService" interface="com.example.MyServiceI">
> >         <invoke-factory>
> >                 <construct class="com.example.MyService"
service-id-property="aProperty"/>
> >         </invoke-factory>
> >  </service-point>
> >
> 
> thanks, injecting the service-id works well for me. (and it is cool :)
> 
> > Now we have support natively in HiveMind 1.1 for serialization with 
> > a similar approach but you have to call registry.setupThread() 
> > before the functionality can be used on a given thread.
> 
> seems like it worked fine without setupThread in alpha-2 (but not in 
> beta-1)... but I suspect the reason for this change was this: "Allow 
> for running with multiple Registries. (HLS) Fixes HIVEMIND-83"
> 
> thanks,
>  viktor
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> 
> 


-- 
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Howard Lewis Ship <hl...@gmail.com>.
BTW ... you can have HiveMind inject the service proxy into the
service implementation, just like any other collaborating service.

On 5/9/05, Viktor Szathmary <ph...@gmail.com> wrote:
> On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> >
> > Or better:
> >
> >  <service-point id="MyService" interface="com.example.MyServiceI">
> >         <invoke-factory>
> >                 <construct class="com.example.MyService" service-id-property="aProperty"/>
> >         </invoke-factory>
> >  </service-point>
> >
> 
> thanks, injecting the service-id works well for me. (and it is cool :)
> 
> > Now we have support natively in HiveMind 1.1 for serialization with a similar
> > approach but you have to call registry.setupThread() before the functionality
> > can be used on a given thread.
> 
> seems like it worked fine without setupThread in alpha-2 (but not in
> beta-1)... but I suspect the reason for this change was this: "Allow
> for running with multiple Registries. (HLS) Fixes HIVEMIND-83"
> 
> thanks,
>  viktor
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> 
> 


-- 
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Viktor Szathmary <ph...@gmail.com>.
On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> 
> Or better:
> 
>  <service-point id="MyService" interface="com.example.MyServiceI">
>         <invoke-factory>
>                 <construct class="com.example.MyService" service-id-property="aProperty"/>
>         </invoke-factory>
>  </service-point>
> 

thanks, injecting the service-id works well for me. (and it is cool :)

> Now we have support natively in HiveMind 1.1 for serialization with a similar
> approach but you have to call registry.setupThread() before the functionality
> can be used on a given thread.

seems like it worked fine without setupThread in alpha-2 (but not in
beta-1)... but I suspect the reason for this change was this: "Allow
for running with multiple Registries. (HLS) Fixes HIVEMIND-83"

thanks,
 viktor

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Pablo Lalloni <pl...@afip.gov.ar>.
El Lun 09 May 2005 18:01, Viktor Szathmary escribió:
> I'm ok with injecting the service-id from the constructor, but how
> does hivemind help in this?  Are you saying I need to copy-paste it,
> as below? That's not very cool :)
>
> <service-point id="MyService" interface="com.example.MyServiceI">
> 	<invoke-factory>
> 		<construct class="com.example.MyService">
> 			<string>com.example.MyService</string>
> 		</construct>
> 	</invoke-factory>
> </service-point>

You can do just:

 <service-point id="MyService" interface="com.example.MyServiceI">
 	<invoke-factory>
 		<construct class="com.example.MyService">
 			<service-id/>
 		</construct>
 	</invoke-factory>
 </service-point>

Or better:

 <service-point id="MyService" interface="com.example.MyServiceI">
 	<invoke-factory>
 		<construct class="com.example.MyService" service-id-property="aProperty"/>
 	</invoke-factory>
 </service-point>

Or better yet, if you're using auto-wiring and your service implementation 
class has a setServiceId(String) method, then HiveMind will automatically 
invoke it, with the service-id value set on the parameter, during service 
initialization, and this is quite cool, isn't it? :-)

> The reason for passing around the proxy instead of the actual instance
> is that these objects get serialized and need to reconnect to the
> appropriate service. This is what the the proxy does automatically,
> but I'm ok with resolving it myself if there's no other way (but
> that's also not quite as cool :)

In the past when I had to serialize services (and there wasn't native support 
for this on HiveMind) I just added a writeReplace() method on the services 
base class, and on the services base interface so BuilderFactory adds it to 
the proxy, this method just returned a POJO containing the service-id (which 
was injected with auto-wiring) and with a readResolve() method that, upon 
deserialization, returned the real service which it looked up in the 
registry.

That was enough to serialize/deserialize a service instance.

Now we have support natively in HiveMind 1.1 for serialization with a similar 
approach but you have to call registry.setupThread() before the functionality 
can be used on a given thread.

Hope this helps...

>
> regards,
>   viktor
>
> On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> > El Lun 09 May 2005 17:01, Viktor Szathmary escribió:
> > > hi,
> > >
> > > how can a hivemind-managed object refer to it's "real self"? for
> > > example, if I need to know the service ID, or want to be able to pass
> > > the proxied instance from the service itself to another object...
> >
> > For knowing the service-id just use a constructor parameter or make your
> > service implementation class implement a setServiceId(String id), then
> > hivemind can inject it.
> >
> > For passing around the proxy... your really shouln't be needing to do
> > such a thing... how come? :-)
> >
> > > btw, i have briefly tried beta-1 (instead of alpha-3), and it seems
> > > service serialization broke - it keeps complaining about "instance not
> > > created in this VM".. since i didnt have time to focus on this, i just
> > > rolled back to alpha-3...
> >
> > Maybe you aren't calling registry.setupThread() before using
> > deserialization on a given thread?
> >
> > --
> > Pablo I. Lalloni <pl...@afip.gov.ar>
> > Teléfono +54 (11) 4347-3177
> > Proyecto Pampa
> > Dirección Informática Tributaria
> > AFIP
> >
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > Nudists are people who wear one-button suits.

-- 
Pablo I. Lalloni <pl...@afip.gov.ar>
Teléfono +54 (11) 4347-3177 
Proyecto Pampa
Dirección Informática Tributaria
AFIP

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To be a kind of moral Unix, he touched the hem of Nature's shift.
		-- Shelley

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Viktor Szathmary <ph...@gmail.com>.
I'm ok with injecting the service-id from the constructor, but how
does hivemind help in this?  Are you saying I need to copy-paste it,
as below? That's not very cool :)

<service-point id="MyService" interface="com.example.MyServiceI">
	<invoke-factory>
		<construct class="com.example.MyService">
			<string>com.example.MyService</string>
		</construct>
	</invoke-factory>
</service-point>

I would like to avoid manually resolving & duplicating service ids in
my hive-config files...

The reason for passing around the proxy instead of the actual instance
is that these objects get serialized and need to reconnect to the
appropriate service. This is what the the proxy does automatically,
but I'm ok with resolving it myself if there's no other way (but
that's also not quite as cool :)

regards,
  viktor

On 5/9/05, Pablo Lalloni <pl...@afip.gov.ar> wrote:
> El Lun 09 May 2005 17:01, Viktor Szathmary escribió:
> > hi,
> >
> > how can a hivemind-managed object refer to it's "real self"? for
> > example, if I need to know the service ID, or want to be able to pass
> > the proxied instance from the service itself to another object...
> 
> For knowing the service-id just use a constructor parameter or make your
> service implementation class implement a setServiceId(String id), then
> hivemind can inject it.
> 
> For passing around the proxy... your really shouln't be needing to do such a
> thing... how come? :-)
> 
> >
> > btw, i have briefly tried beta-1 (instead of alpha-3), and it seems
> > service serialization broke - it keeps complaining about "instance not
> > created in this VM".. since i didnt have time to focus on this, i just
> > rolled back to alpha-3...
> 
> Maybe you aren't calling registry.setupThread() before using deserialization
> on a given thread?
> 
> --
> Pablo I. Lalloni <pl...@afip.gov.ar>
> Teléfono +54 (11) 4347-3177
> Proyecto Pampa
> Dirección Informática Tributaria
> AFIP
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Nudists are people who wear one-button suits.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


Re: referencing "self" in a service

Posted by Pablo Lalloni <pl...@afip.gov.ar>.
El Lun 09 May 2005 17:01, Viktor Szathmary escribió:
> hi,
>
> how can a hivemind-managed object refer to it's "real self"? for
> example, if I need to know the service ID, or want to be able to pass
> the proxied instance from the service itself to another object...

For knowing the service-id just use a constructor parameter or make your 
service implementation class implement a setServiceId(String id), then 
hivemind can inject it.

For passing around the proxy... your really shouln't be needing to do such a 
thing... how come? :-)

>
> btw, i have briefly tried beta-1 (instead of alpha-3), and it seems
> service serialization broke - it keeps complaining about "instance not
> created in this VM".. since i didnt have time to focus on this, i just
> rolled back to alpha-3...

Maybe you aren't calling registry.setupThread() before using deserialization 
on a given thread?

-- 
Pablo I. Lalloni <pl...@afip.gov.ar>
Teléfono +54 (11) 4347-3177 
Proyecto Pampa
Dirección Informática Tributaria
AFIP

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nudists are people who wear one-button suits.

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org


RE: referencing "self" in a service

Posted by James Carman <ja...@carmanconsulting.com>.
What type of "other object" are you referring to?

-----Original Message-----
From: Viktor Szathmary [mailto:phraktle@gmail.com] 
Sent: Monday, May 09, 2005 4:01 PM
To: hivemind-user@jakarta.apache.org
Subject: referencing "self" in a service


hi,

how can a hivemind-managed object refer to it's "real self"? for example, if
I need to know the service ID, or want to be able to pass the proxied
instance from the service itself to another object...

btw, i have briefly tried beta-1 (instead of alpha-3), and it seems service
serialization broke - it keeps complaining about "instance not created in
this VM".. since i didnt have time to focus on this, i just rolled back to
alpha-3...

regards,
  viktor

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org