Announcement Announcement Module
Collapse
No announcement yet.
RemoteServiceExporter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RemoteServiceExporter

    Hi,

    I have been a stray outsider since some time just watching the forum and learning more everyday. However, today I have come across a real issue and would really appreciate if anyone out there could help me with this.

    I have a bean in my spring context which is a service with @Transactional annotation. However, I also want to expose it using a RmiServiceExporter for remoting.

    To do so, I created
    1. An annotation called @Remote like so
    Code:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Remote {
    	Class<?> serviceInterface();
    	Profile profile() default Profile.TEST;
    }
    2. An enumeration of profiles like this
    Code:
    public enum Profile {
    	
    	TEST, PRODUCTION;
    
    }
    3. Used the @Remote annotation like this on the service class
    Code:
    @Transactional
    @Remote(serviceInterface=Service.class)
    public class ServiceImpl implements Service{
    @Inject
    private XDao dao;
    public void doSomething(){
    ...
    }
    }
    4. Wrote a BeanPostProcessor to generate a Remote Service infrastructure for the @Remote Annotation like so
    Code:
    public class RemoteAnnotationBeanPostProcessor implements BeanPostProcessor {
    	private int registryPort = 1099;
    
    	private static final String activeProfileProperty = System.getProperty("profile") == null ? Profile.TEST.toString() : System.getProperty("profile");
    
    	public Object postProcessBeforeInitialization(Object bean, String beanName)
    			throws BeansException {
    		return bean;
    	}
    
    	public Object postProcessAfterInitialization(Object bean, String beanName)
    			throws BeansException {
    		final Class<?> clazz = bean.getClass();
    		final Remote remoteAnnotation = clazz.getAnnotation(Remote.class);
    		if (null != remoteAnnotation) {
    			try {
    				Profile activeProfile = remoteAnnotation.profile();
    				activeProfile = Profile.valueOf(activeProfileProperty);
    				if (remoteAnnotation.profile().equals(activeProfile)) {
    					RmiServiceExporter exporter = new RmiServiceExporter();
    					exporter.setServiceName(beanName);
    					exporter.setService(bean);
    					exporter.setServiceInterface(remoteAnnotation.serviceInterface());
    					exporter.setRegistryPort(registryPort);
    					exporter.afterPropertiesSet();
    				}
    			} catch (RemoteException e) {
    				e.printStackTrace();
    			}
    		}
    		return bean;
    	}
    
    	public void setRegistryPort(int registryPort) {
    		this.registryPort = registryPort;
    	}
    }
    But my problem is that I am not able to get the annotation on the class as spring initializes a transactional proxy instead of the real serviceImpl class, hence the service doesnt get exported as a remote service.

    What am I missing? And how do I get it work without writing the RmiServiceExporter bean in the sml file???

    Help would be greatly apprecaited.

  • #2
    Use the Spring supplied Aop(Proxy)Utils to retrieve the actual class of the bean instead of the proxy.

    Code:
    Class<?> clazz = AopProxyUtils.ultimateSuperClass(bean);

    Comment


    • #3
      Thanks,

      I will try to use that.

      However, for Transactional-proxy beans, is this the right place to expose them as remote services?

      Comment

      Working...
      X