Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Generating the Thrift tutorial to Java, generates an interface Calculator.Iface where each method declares to throw a TException. Why?
Why not just keep it clean with business logic?
EDIT: I guess the motivation is to know if something happened in the Thrift invocation. So why not make it a runtime exception? Anyone has a link to a document/conversation brain-storming Thrift's exceptions?
I guess the motivation is to know if something happened in the Thrift
invocation. So why not make it a runtime exception?
One reason that could have driven the decision is that it is easy to mis-configure a middle-man message protocol (like thrift or protobuf) with the wrong file that defines a structure. Protobuf for example also throws an InvalidProtocolBufferException that extends IOException when you try to parse ByteString into a structure thereby forcing you to handle it.
I'm not a fan of checked exceptions in any form either and have debated it in various forums and discussion arenas at length. A while back, a framework I was working on needed to support multiple message protocol bindings (like thrift, json, protobuf etc). I decided to handle the checked exceptions and throw them as a RuntimeParsingException should I encounter inconsistencies between a client and server structure.
It is not pragmatic to expect a client to handle a specific message level protocol exception (by making it mandatory) when that underlying message protocol can change tomorrow.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Not a duplicate: although similar to other questions about assertions, this question specifically focuses on assertions vs exceptions in libraries to be distributed to 3rd parties and more specifically in a library that does not necessarily indicate a fatal condition to the applications using it.
There are many conflicting answers to other questions which have overlapping use cases, making most answers rather ambiguous to my specific situation.
I maintain Simple Java Mail and in the code I have a few places where I throw an AssertionError, indicating the state has become invalid and I as a library designer cannot guarantee proper functioning. Basically those should never happen and I included those as a failsafe / failfast.
Now a user has raised an issue, indicating to me that this use of java errors is inappropriate because it will potentially halt the entire application and anyway the library user will likely have implemented exception handling somewhere on a case by case basis (paraphrasing). This also makes sense to me.
What is appropriate for a library: AssertionErrors or Exceptions?
Also see this answer to "Is actively throwing AssertionError in Java good practice?", where Google Guava (and Joshua Bloch) seem to be in favor of Errors although it is not entirely clear if this applies to libraries as well.
You have the right idea. In Java you can use Exceptions to indicate when an operation cannot proceed. Examples of this are MalformedURLException, and NumberFormatException.
The pre-built exception I would point you to is IllegalStateException, but you also might way to create your out Exception, in which case you need to think about whether it should extend Exception or RuntimeException.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Due to some policy constraint at our company, we cannot use any external Library. I couldn't find any way to do that in Java.
I can think of two ways to avoid using an external library:
Implement your own SSH File Transfer Protocol client using the standard SSLSocket class ad related classes. The specifications are linked from the Wikipedia page. A brief review of the spec suggests that the protocol is not that complicated.
Identify and install a command-line client for SFTP, then use Process and ProcessBuilder to run the client as an external process.
Before you undertake any significant coding work on this, I would advise you to estimate how much dev time it will take to code test and maintain the code. If it seems like a lot, document the estimates and take them to your line manager.
If you are faced with a significant amount of extra work, it may affect your ability to meet your deadlines. Your manager needs to know about that.
If your manager is faced with a large dev cost or schedule slippage, he or she may be prepared to argue for an exemption to this (IMO) crazy corporate policy.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Recently,I read code of HBase.I find client use protobuf to communicate with server in HBase's code.
Java has "Serializable". why not use it?
Efficiency: protocol buffers are generally much more efficient at transmitting the same amount of data than Java binary serialization
Portability: Java binary serialization is not widely implemented outside Java, as far as I'm aware (unsurprisingly)
Robustness in the face of unrelated changes: unless you manually specify the serializable UUID, you can end up making breaking changes without touching data at all in Java. Ick.
Backward and forward compatiblity: old code can read data written by new code. New code can read data written by old code. (You still need to be careful, and the implications of changes are slightly different between proto2 and proto3, but basically protobuf makes this a lot easier to reason about than Java.)
It's a lot easier to accidentally introduce non-serializable members into Java binary serialization, as proto descriptor files are all about serialization... you can't refer to an arbitrary class, etc.
I've worked on projects using protocol buffers, and I've worked on projects using Java binary serialization - and I would be very reluctant to use the latter again...
Protocol Buffers is an open serialization protocol. You could write a client in C++ or C# and still be able to communicate with the server if both ends are using the same Protocol Buffer schema. Java Serializable is Java only
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to build some processes to form a distributed system.
I am in a dillema between RMI and JMS.
Issues:
I opted RMI since I already know it and it fits the distributed systems and it is fast. But the problem is that it is blocking.
I.e. if one of the other process hangs the calling process will be "stuck" on the method call. I think there are some third party libraries but I don't know if they are stable enough.
JMS is a standard and avoids the problem since it is asynchronous. But going this way I have the following issue (also I haven't used JMS before):
If I send a message to one of the processes, I sometimes (depending on the context/flow) need to know that the other process actually did something after receiving my message. But this forms a "synchronous" model, right?
So taking all these into account, what would be the best approach and how my problems would be solved in each case? E.g. my problem with JMS how would it be solved?
JMS is a better solution because of the reasons you mentioned.
Asynchronous
Non Blocking
For receiving acknowledgements you could have the receiver send you messages post some action.
The Actor model which builds on message processing concepts is worth mentioning here.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I currently need to resolve an issue with duplicated logic on web-based monitoring (Java) and a big legacy C server app.
For this I need to build new clients for the C app, but I have no idea what formats are good for Java to read.
Should I use XML, Json, or some other format?
The answer is completely dependent on your problem domain. Java has libraries available for reading XML, JSON and a host of other protocols.
You need to be asking questions like:
How much data will I be producing?
Does the data need to be human-readable?
Is storage size an issue?
Is the time to read / write the data an issue?
Do I need to support multiple, versioned protocols?
You can use either, JSON is the new standard for web-based messages and there are plenty of Java libraries to handle JSON efficiently.
There's no one stop perfect fit here, but maybe Google's protobuf is a good idea?
There's a native C++ compiler, which is probably of no use to you; there's an active protobuf-c implementation that you might be able to use, though.