Error creating a field with functions when using Apache Thrift - java

I started to use apache thrift (programming for java) and It's very hard to find documentation which explain in deeply about it - so I hope you'll be able to help me.
I'm trying to make an service (interface) which has a function that return a field with functions (for example: another interface).
I tried this code:
namespace java test
service A {
string somefunc()
}
service B {
string somefunc2(),
A getA()
}
But I didn't success.. when I try to compile the thrift file I get an error that it in service B - A field is not defined.
I tried also:
namespace java test
struct A {
1: string somefunc()
}
service B {
A getA()
}
This time it compiled successfully but it didn't count somefunc as a function however as a field in type of string.
Is there anyway make something like what I want?
Thanks!

Thrift sends serialized data structures over the wire. There's no standart way to send executable code. Various languages allow transferring code over the wire (i.e., java .class file or python script in text form) but can't be made interoperable and thus not supported by thrift.
However, thrift might be used for service discovery, if it's what you need. Single thrift service is always bound on specific host/port. So, thrift definition for service discovery code might look like this:
namespace java test
struct Endpoint {
1: required string host;
2: required i32 port;
}
service A {
string somefunc()
}
service B {
string somefunc2(),
Endpoint getA()
}
The service discovery code might look like this:
B.Client bClient = <.....>
Endpoint endpoint = bClient.getA();
TTransport transport = new TSocket(endpoint.host, endpoint.port);
transport.open();
A.Client aClient = = new A.Client(new TBinaryProtocol(transport));
aClient.somefunc2();
If it's needed, Endpoint definition might be extended with protocol/transport metadata allowing to choose between binary/compact/JSON protocols and TTransport/TFramedTransport/etc..

Related

Read complete type structure while loading WSDL from Apache CXF using its internal class WSDLManager?

I'm trying to solve this interesting problem of converting all WSDL definitions to Open API/Swagger like API metadata. For this I zeroed on Apache CXF and found we have WSDLManager class implementation to parse WSDL files. I was able to get services, porttype, operations and messages. However, getting final message parameters and return types seems to be not straight forward.
Here is the snippet
public void parse(string wsdlPath){
Bus bus = BusFactory.getThreadDefaultBus();
WSDLManager man = bus.getExtension(WSDLManager.class);
Definition df = man.getDefinition(wsdlPath);
}
Any pointers will be greatly helpful.

Calling a web service that accept array or list of a class in Java

I want to create a web service using C#. In web service I have a web method that accept list of a specific class:
[DataContract]
public class CompositeType
{
string stringValue = "Hello ";
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
[DataMember]
public List<Product> Products { get; set; }
}
[DataContract]
public class Product
{
[DataMember]
public int PID { get; set; }
[DataMember]
public string PName { get; set; }
}
and my web method:
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
I want to publish this service using BasicHttpBinding that Java users can call it too. Now since the Java programmer is not around me, I wanted to ask those who have the experience to do this :
1) Can Java programmers call my web method that accept List<Product>?
2)Should I change List to Array?
Thanks for you contribution
Presumably your HTTP API serializes this as JSON (or maybe XML). In either case, libraries such as Jackson can handle it just fine, and most REST clients will even handle that part automatically. Standards compliance is the rule, and so as long as your List<Product> is converted to/from a regular JSON array, everything should work smoothly.
JSON doesn't have separate list types, just the plain array, so either array or list-based serialization should be equivalent.
As a note, most APIs use either camelCase or snake_case for properties, so your property names (in JSON) would be expected to be stringValue, products, pid, and pName.
As with .Net client calling WCF using the Svcutil tool, most Java users use the asis2 library which is a webservice engine to invoke web service.
WebService is a specification that any service that implements it can be called WebService. they use SOAP message based on XML to communicate. they use WSDL to describe the service details, which is used for generating the client proxy class. The reason why WCF can be called across service boundaries by various platforms is that it is also a web service. Although there may be different data types on various platforms, as long as we specify how to represent it in XML and how to serialize it, the service can be called correctly by others platforms, By default, List is specified to be serialized using a one-dimensional array.

Communication between Java programs with non-JDK objects

I'm looking for a communication channel between two java programs running on the same machine. I've found a few options (RMI and XML-RCP) but none of the examples that I found show exchange of objects wich class it's non-primitive and not know on JDK (our own objects).
So, what's the easy technology to use when I want to do this (note that Utils.jar it's on the classpath of Server.jar and Client.jar):
Utils.jar:
class MyClassRequestParams { ... }
class MyClassReturnParams { ... }
Client.jar:
// Server creation
...
// Send request
MyClassRequestParams params = new MyClass...
MyClassReturnParams response = server.send("serverMethodName", params);
Server.jar:
MyClassRequestParams serverMethodName(MyClassRequestParams params)
{
MyClassReturnParams response = new MyC...
// do processing
return response;
}
Just make your transport classes implement the Serializable interface, and everything will be fine with RMI. Note that every object referenced bt the transport object should also be Serializable.
The RMI tutorial uses an example with a custom Task interface implemented by a Pi custom class that is not a "standard" JDK class.
You may also consider Versile Java (I am one of its developers). Follow the link for an example of making remote calls and defining remote interfaces. It implements a platform-independent standard for remote ORB interaction, currently also available for python.

Sending an Object over a network using Java

I am trying to send an object in java over a physical network (not over localhost) but it seems I have something wrong.
The interface to the object (client and server have this):
public interface distributable extends Serializable {
public void test();
}
The Object I am trying to send (only server has this):
class ObjectToSend implements distributable {
public ObjectToSend() {
}
public void test() {
system.out.println("worked!");
}
}
Server:
private ObjectToSend obj = new ObjectToSend();
obj_out_stream.writeObject(obj);
obj_out_stream.flush();
Client:
private distributable ReceivedObj = null;
try {
ReceivedObj = (distributable) obj_in_steam,readObject();
} catch (ClassNotFoundException e) {
System.err.println("Error<w_console>: Couldn't recieve application code!");
}
ReceivedObj.test();
Everything was working when the ObjectToSend class implemented Serializable and I wasn't using an interface because all my classes were in one directory so the client 'knew' about the object. Now I want it to work across a physical network so the client only has the interface to the object. It seems that client can not receive the object as the exception is thrown every time.
As the other answers suggest, the Client also has to know the class of the object you want to send.
Usually, one creates three packages/projects for such a classic client-server example:
Common: Code that is used by client and server; the class definition of the objects you want to send from the server to the client belongs here
Client: All code only the client needs to know about
Server: All code only the server needs to know about
To be able to serialize and deserialize objects with objectinput/outputstream the classes must implement Serializable.
Also the deserializer must be able to find the classes on the classpath that you are trying to deserialize since this is embedded in the Serialized form.
If you want the client to have only the interface -- at compile time -- then you'll need to download the actual class from the server at run-time.
Jini (aka Apache River) makes this easy.
It's supposed to be like this. What can you do with a class whose code you don't have?
Have a look here: stackoverflow.com/questions/8175052/java-polymorphism-my-teacher-claims-you-can-distribute-an-executable-object-thr

How can I handle Java object serialization with JavaScript?

public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
private void calculate()
{
}
}
I have serialized the Java object located on the server side and sent it to the client
XStream xstream = new XStream(new DomDriver());
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
How can I deserialize that XML string into the Java object using JavaScript
and execute the methods of person class in the client side using the JavaScript?
Please help me with syntax or any guidelines.
You can call Java methods on the client side using JavaScript by using SOAP. This article explains how to create a WSDL web service that can be accessed by any SOAP client that supports WSDL.
You can then call the Java WSDL service using AJAX in JavaScript (if you can find a JS library that implements SOAP and WSDL).
Alternatively, you can write a simplified front-end to the Java WSDL service in PHP using PHP's built-in SoapClient library. Make it take some simple GET arguments and return JSON or XML. You could then trivially access the PHP web service using AJAX via jQuery (or an equivalent AJAX-supporting library).
If you're going for an applet and want to make Javascript calls from Java, checkout the LiveConnect with the JSObject wrapper class. This way you can excute javascript functions inside the applet (and pass information in between);
Executor exe = Executors.newSingleThreadExecutor();
final JSObject page = JSObject.getWindow(applet);
if (page == null) {
/* Break here, no connection could be made */
}
final String javascriptFunction = "yourJavaScriptFunction()";
executor.execute(new Runnable() {
public void run() {
page.eval(javascriptFunction);
}
});
Look into the IRIS applictation made for Flickr, it's open source and uses this technique. The Belgian JUG Parleys have a speech from a convention covering some of this, You can find it here.
XML is presented as a DOM tree to JavaScript
You can't run Java methods with Javascript. The only thing you could do is to read the properties of the Java object - this is the only information that is serialized in the XML file. It is very easy to read XML with javascript.
To be able to serialize a Java object, send it to a client and execute Java code there a totally different architecture would be needed. At first you need Java running on the client too. Then you would need to employ a method like RMI.

Categories

Resources