Debate over static methods
Hello guys!We have a debate about use static methods or not use static methods.
In this case it is in a EJB-context, (but I do not think that matter, correct me if I'm wrong).
What do you think is the best way or pattern for this code below.
We have a method in a class where the class has no fields, similar to this.
imports ...
public class JmsUtil
{
public String sendBytesMessage( byte[] messageBytes ) throws JMSException, NamingException
{
String jmsMessageID =null;
Connection connection = null;
try
{
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup( "ourfactory" );
Queue queue = (Queue)context.lookup( "ourqueue" );
connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer( queue );
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes( messageBytes );
messageProducer.send( bytesMessage );
jmsMessageID = bytesMessage.getJMSMessageID();
}
finally
{
if(connection != null)
{
connection.close();
}
}
return jmsMessageID;
}
}
Would you prefer to use the code above like:
JmsUtil jmsUtil = new JmsUtil();
String jmsid = jmsUtil.sendBytesMessage( "Hello".getBytes() );
...or change the method to be static and use it like this...
String jmsid = JmsUtil.sendBytesMessage( "Hello".getBytes() );
Is there an pros or cons with one way or the other?
Best regards
Fredrik