Why is JavaMail Transport.send() a static method? - java

I'm revising code I did not write that uses JavaMail, and having a little trouble understanding why the JavaMail API is designed the way it is. I have the feeling that if I understood, I could be doing a better job.
We call:
transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);
So why is Eclipse warning me that this:
transport.send(message, message.getAllRecipients());
is a call to a static method?
Why am I getting a Transport object and providing settings that are specific to it if I can't use that object to send the message? How does the Transport class even know what server and other settings to use to send the message? It's working fine, which is hard to believe. What if I had instantiated Transport objects for two different servers; how would it know which one to use?
In the course of writing this question, I've discovered that I should really be calling:
transport.sendMessage(message, message.getAllRecipients());
So what is the purpose of the static Transport.send() method? Is this just poor design, or is there a reason it is this way?

Transport.send() is basically a convenience method. Yes, if you're managing your own Transport instance, call sendMessage().
I'm not sure I consider it bad design, since frequently you don't care to manage the details of sending and monitoring transport. Crack open the send() method to see what it does for you. Other ways of naming or placing this method could be marginally better.

The javadoc says:
Send is a static method that creates and manages its own connection. Any connection associated with any Transport instance used to invoke this method is ignored and not used. This method should only be invoked using the form Transport.send(msg);, and should never be invoked using an instance variable.
And yes probably, the design is not good

Related

How to analyse Websocket close reason 1011

First time using websockets. I have two machines that need to communicate using them. The server works fine, if I send a message with Postman, it replies correctly.
For the client I used one of the examples I found, like this. But in the client, when I create the WebsocketClientEndpoint :
final WebsocketClientEndpoint clientEndPoint =
new WebsocketClientEndpoint(new URI("ws://myserver.com/endpoint"));
it calls the onOpen and immediately after the onClose, returning a 1011 close reason, that I read is an unexpected condition in the server.
I would need some clue to analyse what can be happening, because as I said, the server replies well in Postman. The url is the same, of course. The examples I find are quite identical, and I am not doing anything different. Any idea?
My fault. As indicated by user207421, I should have checked what was really arriving to the server. The client was not sending hardcoded data. I was sending a JSON to it and it was forwarding it to the server. That server replied well to the same JSON if sent directly. The thing was that the client, in the deserialization and serialization, was sending the final reconstructed JSON with a missing field, and that made the server fail to reply. As dumb as that. The risk of assuming things.
First, I would recommend not to try to instantiate an instance of WebsocketClientEndpoint() as the only implementation of the class I can find uses static connect() methods, which require either an existing instance of WebSocketClient() or WebSocketContainer() - See this example. Instead, I would recommend creating a class that extends WebSocketClient and work with that instead - see an implementation of that here.
Also, another thing that might cause a different sort of problem is the possibility of an Unhandled Exception causing code execution to prematurely abort. The URI class throws a URISyntaxException, and if you are not wrapping your new URI object instantiation in a try/catch block or denoting the current scope method/class as throws URISyntaxException (or throws Exception to handle the other exceptions that might be thrown by WebsocketClient() as well) and have the thrown Exception(s) handled in a try/catch block in outer calling context, your code may be crashing due to that.

Observer Pattern VS Owner Referencing. Which is more correct? (Java)

Within Java you can create an Observer-Observable set of classes in which the Observable can call the Observer. You can also in java explicitly reference an owning class instance in a child instance of another class and call the owning class instance's public functions.
Which is the better approach to take? Which is more beneficial in different scenarios, one example being Multi-Threading?
The Observer Pattern should be used whenever you don't know or don't care who is observing you. This is the key-concept in event-driven programming. You don't have any control of who is observing or what they do when you broadcast your events. Like you already mentioned in your comments, this is great for decoupling classes.
An example of a usage could be in a plugin-architecture:
You write a basic mail-server that broadcasts whenever a mail is received. You could then have a spam-plugin that validates the incoming mail, an auto-reply service that sends a reply, a forward service that redirects the mail and so on. Your plain mail server (the observable) doesn't know anything about spam, replies or forwarding. It just shouts out "Hey, a new mail is here" not knowing if anyone is listening. Then each of the plugins (the observers) does their own special thing, not knowing anything about each other. This system is very flexible and could easily be extended.
But the flexibility provided by the Observer Pattern is a two-edged sword. In the mail-server example, each plugin handles the incoming mail in total isolation from each other. This makes it impossible to setup rules like "don't reply or forward spam" because the observers doesn't know about each other - and even if they did, they wouldn't know in what order they are executed or has completed. So for the basic mail-server to solve this problem, It'll need to have references to the instances that does the spam/reply/forward actions.
So the Observer Pattern provides flexibility. You could easily add a new anti-virus plugin later, without having to modify your plain mail server code. The cost of this flexibility is loss of control of the flow of actions.
The reference approach gives you total control of the flow of actions. However you would need to modify your plain mail server code if you ever need to add support for an anti-virus plugin.
I hope this example gives you some ideas of the pros and cons of each approach.
In regards to multi-threading, one approach isn't favorable over the other.

doBye method of sip servlet not calling while doing the junit testing through SIPDriver

I have written one sipservlet. while doing the testing through SIPdriver, i am seeing doInvite,doResponce method is calling but doBye method is not calling. Please help. Thanks for your support in advance.
Subsequent SIP requests (like the BYE) typically go direct from peer-to-peer (without going through the proxy). To cause your application to receive the BYE and indeed all other subsequent requests, do one of the following:
On receipt of the INVITE, proxy the request having set record route to true on the proxy (request.getProxy().setRecordRoute(true);) first.
Implement your application behaviour as a back-to-back user agent (b2bua). This is a much more complex application model to implement, but does also yield much more control over the SIP sessions.
Hope this helps!

How to mock an outgoing Socket connection?

In integration tests (JDK 6) I'm trying to catch all outgoing TCP connections and mock them. Looks like I should use java.net.Socket#setSocketImplFactory() method. Works fine at the moment, but I can't understand how I can get an access to original factory, in order to instantiate original JDK-provided SocketImpl class. I need this mostly because I want to let some connections to go out freely, without mocking. Can you suggest some manuals/guidelines/instructions about this problem?
Instead of mocking a Socket I would create a Socket service for the Socket to talk to. This can capture all the data written and reply in any manner you wish. It can be run in the same test and possibly in the same thread.
Looking at the source code of the Socket class, there is no original factory - the constructors check to see if factory is null, and if it is, they just assign impl to be a new PlainSocketImpl().
According to the javadoc, you should be able to use SocketFactory#getDefault() to get the default SocketFactory for your environment.
BTW: You might also want to look at this RFE/bug which declares that SocketImplFactory is basically a dead-end: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4245730

what is the real advanteges of RMI call back?

I have read a lot about RMI call back but I need to know the advantages of using that and is there any special code for making RMI callback?
A callback is a pattern whereby the guy you have called can in turn call a method on you. It means that you don't have to ship him all the data he needs in one go: he can ask you, and he can do that when he needs it, which might be a long time after you call him.
Having said that, RMI callbacks aren't much use in practice except within LANs, as callbacks in general are a prima facie security breach and generally disallowed by firewall administrators. So don't get too keen on them.

Categories