mardi 28 juillet 2015

How to Register Instances with Load Balancer using AWS SDK for Java (in JS Rhino)

Hi I'm trying leverage the AWS SDK for Java inside a JS Rhino Shell and have been successful creating an ELB and removing and ELB. (I'm aware there is a SDK for JavaScript but there were platform-related reasons why that wasn't possible for me). I've tried to adapt the Java examples to JavaScript and having some trouble trying to register instances with the load balancer. I get the following error:

org.mozilla.javascript.WrappedException: Wrapped java.lang.Cl*****CastException: java.lang.String cannot be cast to com.amazonaws.services.elasticloadbalancing.model.Instance

There are two parameters that get passed into the register request, LoadBalancerName name and Instances. That request object is then passed to complete the task. However at that point it seems it doesn't like the array of instances although it accepts it during the setInstances command (and rejects plain strings). Some further reading has indicated it may be looking for a Collection instead of an array. I'm not a Java developer so I'm sorry I don't know what it's expecting. Any help is appreciated!

var awsElbName = "elb-10" //string
var awsInstanceId = ["i-bb3c7270"] //array
var registerRequest = new com.amazonaws.services.elasticloadbalancing.model.RegisterInstancesWithLoadBalancerRequest()
registerRequest.setInstances(awsInstanceId);
registerRequest.setLoadBalancerName(awsElbName);
var registerResult = awsElb.registerInstancesWithLoadBalancer(registerRequest); 
        
        org.mozilla.javascript.WrappedException: Wrapped java.lang.Cl*****CastException: java.lang.String cannot be cast to com.amazonaws.services.elasticloadbalancing.model.Instance
                at org.mozilla.javascript.Context.throw*****criptRuntimeEx(Context.java:1754)
                at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
                at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
                at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
                at org.mozilla.javascript.gen._stdin__38._c_script_0(Unknown Source)
                at org.mozilla.javascript.gen._stdin__38.call(Unknown Source)
                at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
                at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3090)
                at org.mozilla.javascript.gen._stdin__38.call(Unknown Source)
                at org.mozilla.javascript.gen._stdin__38.exec(Unknown Source)
                at org.mozilla.javascript.tools.s*****.Main.processSource(Main.java:493)
                at org.mozilla.javascript.tools.s*****.Main.processFiles(Main.java:176)
                at org.mozilla.javascript.tools.s*****.Main$IProxy.run(Main.java:100)
                at org.mozilla.javascript.Context.call(Context.java:489)
                at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:504)
                at org.mozilla.javascript.tools.s*****.Main.exec(Main.java:158)
                at org.mozilla.javascript.tools.s*****.Main.main(Main.java:136)
        Caused by: java.lang.Cl*****CastException: java.lang.String cannot be cast to com.amazonaws.services.elasticloadbalancing.model.Instance
                at com.amazonaws.services.elasticloadbalancing.model.transform.RegisterInstancesWithLoadBalancerRequestMarshaller.marshall(RegisterInstancesWithLoadBalancerRequestMarshaller.java:51)
                at com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient.registerInstancesWithLoadBalancer(AmazonElasticLoadBalancingClient.java:1833)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                at java.lang.reflect.Method.invoke(Unknown Source)
                at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
                ... 15 more

If I get the value of registerRequest all seems good:

registerRequest

{LoadBalancerName: elb-10,Instances: [i-bb3c7270]}




Aucun commentaire:

Enregistrer un commentaire