论坛首页 Java企业应用论坛

需要但没有session等会话信息下的程序开发

浏览 1671 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-19  
    本人最近在做个小实现:
     需求:类似于聊天室,但又为更广,些聊天可以通过手机短信,手机JAVA程序,网页,或者其它一些登陆到平台进行聊天。
     本人想破了脑袋也只能想出这一实现,如有做过这一项目,或者有更好意思的,欢迎发表,谢谢。
    本人的想法:由于此平台可以通过多个不同的服务进行交互。于是认为必须要一个主服务,此主服务可以通过子服务进行接入处理信息接收。主服务只做登陆人员的信息保存(主服务独立,没有session,application,所有登陆的人员信息要进行保存)。子服务可以随意装配到主服务里面去。主服务与不同子服务之间信息交换时有不同的法则,这个法则就是下面所说的命令。
   
package org.pjsh.main;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.pjsh.dao.JpaServerDao;
import org.pjsh.entity.City;
import org.pjsh.entity.Country;
import org.pjsh.entity.Friend;
import org.pjsh.entity.Person;
import org.pjsh.entity.Province;
import org.pjsh.entity.Scene;
import org.pjsh.environment.Area;
import org.pjsh.environment.CityEvn;
import org.pjsh.environment.CountryEvn;
import org.pjsh.environment.ProvinceEvn;
import org.pjsh.environment.SceneEvn;
import org.pjsh.server.Server;
import org.pjsh.server.SocketServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main extends Thread implements ServletContextListener
{
	private Area countryEvn;
	private JpaServerDao jpaServerDao;
	private TreeMap<String, String> map = new TreeMap<String, String>();
	private Logger log = Logger.getLogger(Main.class);

	public Main()
	{
		System.out.println("start server...");
		log.info("主服务启动...");
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		jpaServerDao = (JpaServerDao) ac.getBean("jpaServer");
		Country country = this.jpaServerDao.getCountryDao().getCountry(1);
		if (country != null)
		{
			countryEvn = new CountryEvn(country.getId(), country.getName());
			Set<Province> lProvice = country.getProvinceList();
			if (lProvice.size() > 0)
			{
				for (Province p : lProvice)
				{
					ProvinceEvn pe = new ProvinceEvn(p.getId(), p.getName());
					Set<City> lCity = p.getCityList();
					for (City c : lCity)
					{
						CityEvn ce = new CityEvn(c.getId(), c.getName());
						Set<Scene> lScene = c.getSceneList();
						for (Scene s : lScene)
						{
							SceneEvn se = new SceneEvn(
									s.getId(), 
									s.getName(),
									s.getSalutatory());
							ce.addSubArea(se);
						}
						pe.addSubArea(ce);
					}
					countryEvn.addSubArea(pe);
				}
			}
		}
		System.out.println("add command...");
		log.debug("添加命令行。");
		addCommandActuator("DL", "login"); // 添加登陆命令
		addCommandActuator("TC", "logout"); // 添加退出命令
		addCommandActuator("WZ", "getLocation"); // 添加显示位置命令
	}

	public JpaServerDao getJpaServerDao()
	{
		return this.jpaServerDao;
	}

	/**
	 * function: addCommandActuator 添加命令执行器与函数的对应关系
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 */
	private void addCommandActuator(String key, String value)
	{
		map.put(key, value);
	}

	/**
	 * function: getCommandActuator 返回命令执行器
	 * 
	 * @param key
	 *            执行器对应的键
	 * @return String 执行器的名称
	 */
	private String getCommandActuator(String key)
	{
		return map.get(key);
	}

	/**
	 * function: getCommand 从发送来的消息中获取命令
	 * 
	 * @param message
	 *            消息
	 * @return String 返回取得的命令
	 */
	private String getCommand(String message)
	{
		message = message.trim();
		if (message.length() <= 0)
		{
			return null;
		}
		if (message.charAt(0) == '/')
		{
			StringBuffer buf = new StringBuffer();
			for (int i = 1; i < message.length(); i++)
			{
				if (i >= 4)
				{
					break;
				}
				if ((message.charAt(i) >= 'a' && message.charAt(i) <= 'z')
						|| (message.charAt(i) >= 'A' && message.charAt(i) <= 'Z'))
					buf.append(message.charAt(i) + "");
				else
					break;
			}
			if (buf.toString().length() <= 1)
			{
				return null;
			} else
			{
				return buf.toString().toUpperCase();
			}
		} else
		{
			return null;
		}
	}

	private void getLocation(String mobile1, String mobile2, String message)
	{
		Person person = countryEvn.getPerson(mobile1);
		String location = countryEvn.getLocation(person);
		notifySms(null, mobile1, "您所在的位置是:" + location);
	}

	/**
	 * function: resolveCommand 将命令解释成要运行的函数
	 * 
	 * @param command
	 *            命令
	 * @param sourceMobile
	 *            发送者的手机号码
	 * @param destMobile
	 *            接收者的手机号码
	 * @param message
	 *            将命令过滤后的消息
	 */
	public void resolveCommand(String command, String sourceMobile,
			String destMobile, String message, Server server)
	{
		if (command == null || command.length() <= 0)
			return;
		String value = this.getCommandActuator(command);
		Method[] methods = this.getClass().getDeclaredMethods();
		for (Method method : methods)
		{
			if (value.equalsIgnoreCase(method.getName()))
			{

				Class[] classes = method.getParameterTypes();
				Object[] object = new Object[classes.length];
				for (int i = 0; i < classes.length; i++)
				{
					if (classes[i].equals(String.class))
					{
						if (i == 0)
						{
							object[i] = sourceMobile;
						} else if (i == 1)
						{
							if (classes.length >= 3)
							{
								object[i] = destMobile;
							} else
							{
								object[i] = message;
							}
						} else
						{
							object[i] = message;
						}
					} else if (classes[i].equals(Server.class))
					{
						object[i] = server;
					} else
					{

					}
				}
				try
				{
					method.invoke(this, object);
				} catch (Exception e)
				{
					// TODO Auto-generated catch block
					log.error("方法执行失败!");
				}
			}
		}
	}

	/**
	 * function: login 用户登陆
	 */
	private void login(String mobile, Server server)
	{
		if (countryEvn != null)
		{
			Person person = countryEvn.isLogin(mobile);
			if (person == null)
			{
				person = jpaServerDao.getPersonDao().findByMobile(mobile);
				if (person == null)
				{
					this.notifySms(null, mobile, "未注册,请先注册!");
					return;
				}
				person.setServer(server);
				countryEvn.receivePerson(person);
				this.notifyLogin(person);
			} else
			{
				this.notifySms(null, mobile, "您已经登陆平台。您所在的位置是:"
						+ countryEvn.getLocation(person));
			}
		}
	}

	/**
	 * function: login 用户退出登陆
	 */
	private void logout(String mobile)
	{
		if (countryEvn != null)
		{
			Person person = countryEvn.getPerson(mobile);
			countryEvn.deletePerson(person);
			if (person == null)
			{
				this.notifySms(null, mobile, "您还没有登陆平台!");
				return;
			}
			this.notifyLogout(person);
		}
	}

	/**
	 * function: notifyLogout 退出时发出通知消息
	 * 
	 * @param person
	 *            人员信息对象
	 */
	private void notifyLogout(Person person)
	{
		if (person == null)
			return;
		notifySms(null, person.getMobile(), "成功退出,谢谢您的使用。");
		Set<Friend> set = person.getFriendList();
		for (Friend friend : set)
		{
			Person p = countryEvn.getPerson(friend.getId());
			if (p != null)
			{
				notifySms(
						null,
						p.getMobile(),
						friend.getName() == null ? 
							(person.getRealname() == null ? 
								person.getRealname()
								: person.getUsername())
							: friend.getName() + "已经离线!");
			}
		}
	}

	/**
	 * function: notifyLogin 登陆时发出通知消息
	 * 
	 * @param person
	 *            人员信息对象
	 */
	private void notifyLogin(Person person)
	{
		if (person == null)
			return;
		Set<Friend> set = person.getFriendList();
		for (Friend friend : set)
		{
			Person p = countryEvn.getPerson(friend.getId());
			if (p != null)
			{
				notifySms(
						null,
						p.getMobile(),
						friend.getName() == null ? 
							(person.getRealname() == null ?
								person.getRealname()
								: person.getUsername())
							: friend.getName() + "上线了!");
			}
		}
	}

	/**
	 * function: notifySms 消息发送
	 * 
	 * @param mobile
	 *            接收消息的手机号码
	 * @param msgStr
	 *            发送的消息
	 */
	private void notifySms(String sourceMobile, String destMobile,
			String message)
	{
		log.debug("消息来自手机:" + sourceMobile + ";目的手机:" + destMobile + ";消息:"
				+ message);
		if (destMobile == null || message == null)
		{
			log.debug("手机号码或者要发送的信息为空!");
		} else
		{
			log.debug("mobile=" + destMobile + ";message=" + message);
			countryEvn.notifySms(sourceMobile, destMobile, message);
		}
	}

	public void run()
	{
	}

	public void contextDestroyed(ServletContextEvent arg0)
	{
		// TODO Auto-generated method stub

	}

	// Tomcat 启动时自动加载
	public void contextInitialized(ServletContextEvent arg0)
	{
		// 插入SocketServer子服务
		SocketServer ss = new SocketServer(this);
		ss.start();
		System.out.println("start server success!");
		log.info("主服务启动结束");
	}
}
    
   发表时间:2009-10-19  
没怎么看明白 楼主能多些文字说明吗?
0 请登录后投票
   发表时间:2009-10-19  
我只看了 上面需求部分

需求:类似于聊天室,但又为更广,些聊天可以通过手机短信,手机JAVA程序,网页,或者其它一些登陆到平台进行聊天。

个人觉得LZ搞懂了边界接入 就可以很好的解决这个问题.
0 请登录后投票
   发表时间:2009-10-23  
什么是边界接入?可以讲解下吗,或者给些资料,我在网上找不到边界接入的相关资料。谢谢
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics