2016-04-13 8 views
5

के साथ पैरामीटर वाले जावा अक्का अभिनेता मेरी परियोजना में, मैंने Lightbend activator template कोड कोड के रूप में उपयोग किया है। यह सही काम करता है लेकिन उदाहरण में अभिनेता पैरामीटर के साथ नहीं बनाया गया है।वसंत

मैं एक नया अभिनेता बना सकते हैं और इसे करने के लिए इस तरह के रूप में निर्माण के दौरान एक पैरामीटर पारित करने के लिए की जरूरत है:

getContext().actorOf(SpringExtProvider.get(actorSystem).props("ControllerActor",type), "controller_" + type) 

इस प्रयोग के मामले में, नियंत्रक प्रकार paremeter एक रंगमंच की सामग्री के साथ बनाया जा करने में सक्षम होने की जरूरत है जिसका उपयोग नियंत्रक (स्पष्ट रूप से) टाइप करने के लिए किया जाता है। प्रत्येक अभिनेता विशेष रूप से अपने प्रकार के आधार पर वस्तु के एक विशिष्ट राजा को संभालने और नियंत्रित करने के लिए डिज़ाइन किया गया है।

लेकिन मैं इस पैरामीटर को पारित करने के लिए प्रोप विधि में नया पैरामीटर नहीं जोड़ सकता। यह काम नहीं कर रहा है।

यह मेरा कोड है:

SpringExtension.java

package com.orange.spectre.core.akka.configuration; 

import akka.actor.AbstractExtensionId; 
import akka.actor.ExtendedActorSystem; 
import akka.actor.Extension; 
import akka.actor.Props; 
import com.orange.spectre.core.config.SpringActorProducer; 
import org.springframework.context.ApplicationContext; 

/** 
* Created by Hervé Darritchon on 04/04/2016. 
* <p> 
* An Akka Extension to provide access to Spring managed Actor Beans. 
*/ 
public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> { 

    /** 
    * The identifier used to access the SpringExtension. 
    */ 
    public static SpringExtension SpringExtProvider = new SpringExtension(); 

    /** 
    * Is used by Akka to instantiate the Extension identified by this 
    * ExtensionId, internal use only. 
    */ 
    @Override 
    public SpringExt createExtension(ExtendedActorSystem system) { 
     return new SpringExt(); 
    } 

    /** 
    * The Extension implementation. 
    */ 
    public static class SpringExt implements Extension { 

     private volatile ApplicationContext applicationContext; 

     /** 
     * Used to initialize the Spring application context for the extension. 
     * 
     * @param applicationContext 
     */ 
     public void initialize(ApplicationContext applicationContext) { 
      this.applicationContext = applicationContext; 
     } 

     /** 
     * Create a Props for the specified actorBeanName using the 
     * SpringActorProducer class. 
     * 
     * @param actorBeanName The name of the actor bean to create Props for 
     * @return a Props that will create the named actor bean using Spring 
     */ 
     public Props props(String actorBeanName) { 
      return Props.create(SpringActorProducer.class, 
        applicationContext, actorBeanName); 
     } 

     public Props props(String actorBeanName, String type) { 
      return Props.create(SpringActorProducer.class, 
        applicationContext, actorBeanName,type); 
     } 
    } 
} 

SpringActorProducer पैकेज com.orange.spectre.core.config;

import akka.actor.Actor; 
import akka.actor.IndirectActorProducer; 
import org.springframework.context.ApplicationContext; 

/** 
* Created by Hervé Darritchon on 21/03/2016. 
*/ 
public class SpringActorProducer implements IndirectActorProducer { 

    private final ApplicationContext applicationContext; 
    private final String actorBeanName; 
    private final String type; 

    public SpringActorProducer(ApplicationContext applicationContext, 
           String actorBeanName) { 
     this.applicationContext = applicationContext; 
     this.actorBeanName = actorBeanName; 
     this.type = null; 
    } 

    public SpringActorProducer(ApplicationContext applicationContext, 
           String actorBeanName, String type) { 
     this.applicationContext = applicationContext; 
     this.actorBeanName = actorBeanName; 
     this.type = type; 
    } 

    @Override 
    public Actor produce() { 
     return (Actor) applicationContext.getBean(actorBeanName); 
    } 

    @Override 
     public Class<? extends Actor> actorClass() { 
     return (Class<? extends Actor>) applicationContext.getType(actorBeanName); 
    } 
} 

के रूप में यह की तरह (Documentation) अक्का के साथ मूल रूप से संभव है मैं एक रंगमंच की सामग्री पैरामीटर के साथ एक अभिनेता नहीं बना सकते:

public class DemoActor extends UntypedActor { 

    /** 
    * Create Props for an actor of this type. 
    * @param magicNumber The magic number to be passed to this actor’s constructor. 
    * @return a Props for creating this actor, which can then be further configured 
    *   (e.g. calling `.withDispatcher()` on it) 
    */ 
    public static Props props(final int magicNumber) { 
    return Props.create(new Creator<DemoActor>() { 
     private static final long serialVersionUID = 1L; 

     @Override 
     public DemoActor create() throws Exception { 
     return new DemoActor(magicNumber); 
     } 
    }); 
    } 

    final int magicNumber; 

    public DemoActor(int magicNumber) { 
    this.magicNumber = magicNumber; 
    } 

    @Override 
    public void onReceive(Object msg) { 
    // some behavior here 
    } 

} 

    system.actorOf(DemoActor.props(42), "demo"); 

क्या आप मेरी मदद कर सकते हैं, यह बहुत अच्छा होना चाहिए!

धन्यवाद।

+0

अच्छा सवाल; उम्मीद है कि आपके प्रयासों का भुगतान होगा! – GhostCat

+0

यदि आप 'SpringActorProducer' में कोड की समीक्षा करते हैं तो कन्स्ट्रक्टर प्रकार पैरामीटर स्वीकार करता है और इसे संपत्ति में संग्रहीत करता है लेकिन फिर 'उत्पाद' विधि जो अभिनेता बनाता है इसका उपयोग नहीं करता है। – nickebbitt

+0

ईमानदार होने के लिए मुझे यकीन नहीं है कि आप जो प्रयास कर रहे हैं वह वास्तव में संभव है। स्प्रिंग कंटेनर में अभिनेता सृजन को सौंपकर मेरा अनुभव यह है कि आपके अभिनेता में वस्तुओं को इंजेक्ट करने के आपके दृष्टिकोण को स्प्रिंग दृष्टिकोण यानी 'ऑटोवायर' का उपयोग करना होगा। अपने अभिनेता में इस प्रकार के इंजेक्शन को इंजेक्ट करने के बजाय यह बताएं कि किस प्रकार की ऑब्जेक्ट्स इसे संभाल सकती हैं, क्यों नहीं, प्रत्येक विशिष्ट ऑब्जेक्ट प्रकार को संभालने के लिए अलग-अलग कलाकारों को परिभाषित नहीं किया जाता है? फिर आप अपनी ऑब्जेक्ट को सही अभिनेता को संसाधित करने के लिए रूट करते हैं – nickebbitt

उत्तर

2

मैं "निकबिट" से सहमत हूं। सुनिश्चित नहीं है कि यह संभव है। और एक तरीका अभिनेता में जादू संख्या जनरेटर के कार्यान्वयन को इंजेक्ट करना है।

ActorSystem.create("system").actorOf(Props.create(SpringDIActor.class, DemoActor.class)) 

तो बस में @Autowired एनोटेशन उपयोग करने के लिए:

public class SpringDIActor implements IndirectActorProducer { 

private static final Logger LOG = LoggerFactory.getLogger(SpringDIActor.class); 

private Class<? extends Actor> type; 
private Actor actorInstance = null; 

public SpringDIActor(Class<? extends Actor> type) { 
    this.type = type; 
} 

public SpringDIActor(Actor actorInstance) { 
    this.actorInstance = actorInstance; 
} 

/** 
* This factory method must produce a fresh actor instance upon each 
* invocation. <b>It is not permitted to return the same instance more than 
* once.</b> 
*/ 
@Override 
public Actor produce() { 
    Actor newActor = actorInstance; 
    actorInstance = null; 
    if (newActor == null) { 
     try { 
      newActor = type.newInstance(); 
     } catch (InstantiationException e) { 
      LOG.error("Unable to create actor of type:{}", type, e); 
     } catch (IllegalAccessException e) { 
      LOG.error("Unable to create actor of type:{}", type, e); 
     } 
    } 
    ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(newActor); 
    return newActor; 
} 

/** 
* This method is used by [[Props]] to determine the type of actor which will 
* be created. This means that an instance of this `IndirectActorProducer` 
* will be created in order to call this method during any call to 
* [[Props#actorClass]]; it should be noted that such calls may 
* performed during actor set-up before the actual actor’s instantiation, and 
* that the instance created for calling `actorClass` is not necessarily reused 
* later to produce the actor. 
*/ 
@Override 
public Class<? extends Actor> actorClass() { 
    return type; 
}} 

यह आपको सह किसी भी कोड से SpringContext के लिए सीधी में जाकर बिना अभिनेताओं बनाने के रूप में इस अनुमति देता है: इसके अलावा, मैं IndirectActorProducer कार्यान्वयन निम्नलिखित सुझाव के wouldlike डेमोक्टर।

डेमोक्टर पर कोई अतिरिक्त एनोटेशन आवश्यक नहीं है।