TodoSessionBean.java


Dosyayı İndir
package com.todo.ejb;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.todo.entity.Category;
import com.todo.entity.Todo;

@Stateless
public class TodoSessionBean implements TodoSessionBeanLocal {
	
	@PersistenceContext(unitName = "TodoJPA")
	private EntityManager entityManager;
	
	public Todo findTodo(int id){
		 
		return entityManager.find(Todo.class, id);

		
	}		
	
	public Todo addTodo(String title,String text,
			int categoryId,Date date){
				
		Todo todo=new Todo();
		
		Category category=new Category();
		category.setId(categoryId);
		todo.setCategory(category);
		
		todo.setDate(date);
		todo.setText(text);
		todo.setTitle(title);
		
		entityManager.joinTransaction();
		entityManager.persist(todo);
		
		// Mesaj kuyruguna todo ekleniyor. yoneticiye todo eklendi maili gonderilebilir. 
		Connection connection=null;
		Session session=null;
		try {

			Context context = new InitialContext();

			QueueConnectionFactory connectionFactory = 
	                (QueueConnectionFactory)context.lookup("ConnectionFactory");
			
			connection=connectionFactory.createConnection();
			
			session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			
			Queue todoQueue = (Queue)context.lookup("queue/MyQueue");
			
			MessageProducer producer=session.createProducer(todoQueue);
			
			ObjectMessage message=session.createObjectMessage();			
			message.setObject(todo);
			
			System.out.println("Todo kuyruga ekleniyor : ");
			producer.send(message);
			System.out.println("Todo kuyruga eklendi : ");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                	e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
            }
        }
		
		return todo;
		
	}
	
	public Todo updateTodo(int id,String title,String text,
			int categoryId,Date date,boolean isactive){
		
		Todo todo=findTodo(id);
		
		Category category=new Category();
		category.setId(categoryId);
		todo.setCategory(category);
		
		todo.setDate(date);
		todo.setText(text);
		todo.setTitle(title);
		todo.setIsactive(isactive);
		
		entityManager.joinTransaction();
		entityManager.merge(todo);
		
		return todo;
		
	}	
	
	public List<Todo> getTodoList(){
		 
		Query query=entityManager.createQuery("select t from Todo as t");
		return (List<Todo>)query.getResultList();
		
	}	
	
	public boolean removeTodo(int id){
		 
		entityManager.joinTransaction();
		Todo todo = entityManager.getReference(Todo.class, id);
		entityManager.remove(todo);
		return true;
		
	}
	
}

				
Dosyayı İndir

Bu Sayfayı Paylaş:




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir