2/08/2010
8:00:00 PM 0

Send JMS Message Using ActiveMQ

library : activemq-core-5.3.0.jar/commons-logging-1.1.jar/j2ee library
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class Sender 
{    
    public static void main(String[] args) 
    {                                                   
        Connection connection = null;
        
        try 
        {
            String url = "tcp://localhost:9876";
            String subject = "test_mq";
            
            ConnectionFactory factory = new ActiveMQConnectionFactory(url);
            connection = factory.createConnection();  
            connection.start();
                                                                                                                                        
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            Destination destination = session.createQueue(subject);
            MessageProducer producer = session.createProducer(destination);                    
            producer.setDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
            producer.setTimeToLive(1000 * 10);
            
            TextMessage message = session.createTextMessage();
            message.setJMSCorrelationID(String.valueOf(System.currentTimeMillis()));
            message.setJMSReplyTo(destination);
            message.setText("Test");            
            producer.send(message);
            
            producer.close();
            session.close();
            connection.stop();
        } 
        catch (JMSException e) 
        {                                                             
            e.printStackTrace();                                                                                                            
        } 
        finally 
        {                                                                            
            if (connection != null) 
            {                                                          
                try { connection.close(); }                                                    
                catch (JMSException e) {}                                                      
            }                                                                                  
        }                                                                                      
    }                                                                                          
}

2/06/2010
2/04/2010
6:26:00 AM 0

設定 Vista 防火牆規則,存取遠端 SQL Server 2005

  1. 開啟進階防火牆設定
  2. 開始→程式集→系統管理工具→具有進階安全性的Windows 防火牆
  3. 建立新規則
  4. 開啟進階安全性防火牆視窗後,請點選『輸入規則』,按滑鼠右鍵,選『新規則』
  5. 規則類型:點選自訂後,按下一步
  6. 程式:點選所有程式後,按下一步
  7. 通訊及連接埠:通訊協定類型選擇 UDP,遠端連接埠選擇特定連接埠,輸
    入 1434 再按『下一步』
  8. 接下來設定一些網域 IP 規則,便大功告成
2/03/2010
8:17:00 PM 0

使用 NHibernate 記得要把 Section Close

最近所維護系統的使用者有越來越多,DataBase 的負擔也越來越重,也因此程式從 SQL Server 取得資料常有 Timeout 的情形,檢查 SQL Server 的 Connection List,赫然發現一支使用 NHibernate 的 UI,程式一啟動就建立了 7~8 個 Connection,狀態都是 Sleep,一直到程式關閉,Connection 才消失,就算使用 SqlConnection.ClearAllPool(),也無法強制將這些多餘的 Connection 回收,查了一下文件 NHibernate 使用 ADO.NET 來對資料庫做存取的動作,所以沒有理由下了 SqlConnection.ClearAllPool(),Connection 卻沒回收,檢查程式後發現,原來是因為 Section 未關閉,以致於 Connection 使用完卻無法有效回收

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
cfg.SetProperty("hibernate.connection.connection_string", "Server=myserver;initial catalog=ABC;User Id=sa;Password=");
cfg.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate");
cfg.SetProperty("hibernate.use_outer_join", "true");
cfg.SetProperty("hibernate.show_sql", "false");

NHibernate.ISessionFactory sessionFactory = cfg.BuildSessionFactory();
using (NHibernate.ISession sess1 = sessionFactory.OpenSession())
{
  ...
}

NHibernate.ISession sess2 = sessionFactory.OpenSession();
try
{
  ...
}
catch (Exception ex)
{

}
finally
{
    sess2.Close();
}

關於 Session Close
A call to ISession.Close() marks the end of a session. The main implication of Close() is that the ADO.NET connection will be relinquished by the session.

關於 Session Flush
Flushing synchronizes the persistent store with in-memory changes but not vice-versa. Note that for all NHibernate ADO.NET connections/transactions, the transaction isolation level for that connection applies to all operations executed by NHibernate.

From time to time the ISession synchronizes its persistent state with the database. Performance will be affected if this process occurs too often. You may sometimes minimize unnecessary flushing by disabling automatic flushing or even by changing the order of queries and other operations within a particular transaction.

sess.Save(cat);
sess.Flush(); //force the SQL INSERT
sess.Refresh(cat); //re-read the state (after the trigger executes)

DomesticCat cat = (DomesticCat) sess.Load( typeof(Cat), 69L ); cat.Name = "PK" sess.Flush(); // changes to cat are automatically detected and persisted

參考資料:http://www.hibernate.org/