Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package org.torquebox.messaging.client;
  2.  
  3. import javax.jms.Connection;
  4. import javax.jms.ConnectionFactory;
  5. import javax.jms.JMSException;
  6. import javax.jms.MessageProducer;
  7. import javax.jms.Session;
  8. import javax.naming.Context;
  9. import javax.naming.NamingException;
  10.  
  11. public class Client {
  12.  
  13. private Context context;
  14.  
  15. private ConnectionFactory connectionFactory;
  16. private Connection connection;
  17. private Session session;
  18.  
  19. public Client() {
  20.  
  21. }
  22.  
  23. public void setContext(Context context) {
  24. this.context = context;
  25. }
  26.  
  27. public Context getContext() {
  28. return this.context;
  29. }
  30.  
  31. public void setConnectionFactory(ConnectionFactory connectionFactory) {
  32. this.connectionFactory = connectionFactory;
  33. }
  34.  
  35. public ConnectionFactory getConnectionFactory() {
  36. return this.connectionFactory;
  37. }
  38.  
  39. public Connection getConnection() {
  40. return this.connection;
  41. }
  42.  
  43. public Session getSession() {
  44. return this.session;
  45. }
  46.  
  47. protected void create() throws JMSException {
  48. this.connection = getConnectionFactory().createConnection();
  49. this.session = this.connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
  50. }
  51.  
  52. protected void start() throws JMSException {
  53. if (this.connection != null) {
  54. this.connection.start();
  55. }
  56. }
  57.  
  58. protected void stop() throws JMSException {
  59. if (this.connection != null) {
  60. this.connection.stop();
  61. }
  62. }
  63.  
  64. protected void destroy() throws JMSException {
  65. if (this.connection != null) {
  66. this.connection.close();
  67. this.connection = null;
  68. }
  69. }
  70.  
  71. public void commit() throws JMSException {
  72. if (this.session == null) {
  73. throw new IllegalStateException("No session");
  74. }
  75. this.session.commit();
  76. }
  77.  
  78. public void rollback() throws JMSException {
  79. if (this.session == null) {
  80. throw new IllegalStateException("No session");
  81. }
  82. this.session.rollback();
  83. }
  84.  
  85. public void connect() throws JMSException {
  86. create();
  87. start();
  88. }
  89.  
  90. public void close() throws JMSException {
  91. if ( this.session != null ) {
  92. commit();
  93. }
  94. stop();
  95. destroy();
  96. }
  97.  
  98. public Object lookup(String name) throws NamingException {
  99. return this.context.lookup( name );
  100. }
  101.  
  102. }
Add Comment
Please, Sign In to add comment